Worship, Revenge, Weirdness
JKR uses Dobby as a deus ex machina, but as always she finds a way to make it plausible. In this case, it's Dobby's unique character and psychology that explains why he pops out of the blue and tries to save Harry from what he thinks is almost certain death. I would argue that it's the result of a combination of factors:
- Worship. As pointed out by @Au101, Harry has become, although not voluntarily or knowingly, a reference point not only for freedom-seeking witches and wizards but for all those who were oppressed under Voldemort's first rule, including goblins and house-elves. This explains why Dobby wanted to help.
However, this would not have sufficed to urge any other house-elves to action. But Dobby is not like "any other":
- Weirdness. As we all know, Dobby is an outlier, a house-elf like no other. He thinks - and acts - autonomously, he doesn't want to serve someone until being beheaded and put up on the wall, and he thinks he has a right to be paid. In short, he considers himself a free house-elf who can interact with witches and wizards on the same level. This is why only he could a) think critically about the threat posed by the Diary, b) feel he - a house-elf - could do something about it, c) leave Malfoy Manor without permission, d) seek the attention of an unknown (and famous) wizard, e) tell Harry about the threat and by so doing betray his masters, f) sabotage and hurt Harry afterwards. And the fact that he would punish himself for all this does not change anything.
“House-elves is not paid, sir!” she said in a muffled squeak. “No, no, no. I says to Dobby, I says, go find yourself a nice family and settle down, Dobby. He is getting up to all sorts of high jinks, sir, what is unbecoming to a house-elf. You goes racketing around like this, Dobby, I says, and next thing I hear you’s up in front of the Department for the Regulation and Control of Magical Creatures, like some common goblin.”
“Well, it’s about time he had a bit of fun,” said Harry.
“House-elves is not supposed to have fun, Harry Potter,” said Winky firmly, from behind her hands. “House-elves does what they is told. I is not liking heights at all, Harry Potter” — she glanced toward the edge of the box and gulped — “but my master sends me to the Top Box and I comes, sir.”
And
“Dobby has traveled the country for two whole years, sir, trying to find work!” Dobby squeaked. “But Dobby hasn’t found work, sir, because Dobby wants paying now!”
The house-elves all around the kitchen, who had been listening and watching with interest, all looked away at these words, as though Dobby had said something rude and embarrassing. Hermione, however, said, “Good for you, Dobby!”
“Thank you, miss!” said Dobby, grinning toothily at her. “But most wizards doesn’t want a house-elf who wants paying, miss. ‘That’s not the point of a house- elf,’ they says, and they slammed the door in Dobby’s face! Dobby likes work, but he wants to wear clothes and he wants to be paid, Harry Potter. … Dobby likes being free!”
The Hogwarts house-elves had now started edging away from Dobby, as though he were carrying something contagious. [...]
- Revenge. Dobby hates the Malfoys. Before becoming free, he serves his masters but clearly against his will, thirsty for freedom. He most likely overhears the family's discussions about Voldemort, Death Eaters (or at least the purity of magical blood and the disgrace Mudbloods represents) and Harry. The latter apparently is one of Dracos favorite arguments:
“… everyone thinks he’s so smart, wonderful Potter with his scar and his broomstick —”
“You have told me this at least a dozen times already,” said Mr. Malfoy, with a quelling look at his son. “And I would remind you that it is not — prudent — to appear less than fond of Harry Potter, not when most of our kind regard him as the hero who made the Dark Lord disappear — ah, Mr. Borgin.”
It is not difficult to imagine that Dobby, aware of his masters' feelings, decided to act not only to save Harry but also to take revenge on the Malfoys when he came to know about Tom Riddle's Diary.