In Harry Potter and the Chamber of Secrets, why is Dobby helping Harry Potter? What are his motives?
Dobby wants to help Harry Potter because he brought about the downfall of Voldemort and things were much worse for his kind under Voldemort and largely better (for his kind) after Voldemort's fall:
'I don't suppose you're going to tell me why you wanted me sent home in pieces?'
'Ah, if Harry Potter only knew!' Dobby groaned, more tears dripping onto his ragged pillowcase. 'If he knew what he means to us, to the lowly, the enslaved, us dregs of the magical world! Dobby remembers how it was when He Who Must Not Be Named was at the height of his powers, sir! We house-elves were treated like vermin, sir! Of course, Dobby is still treated like that, sir,' he admitted, drying his face on the pillowcase. 'But mostly, sir, life has improved for my kind since you triumphed over He Who Must Not Be Named. Harry Potter survived, and the Dark Lord's power was broken, and it was a new dawn, sir, and Harry Potter shone like a beacon of hope for those of us who thought the dark days would never end, sir ...'
Harry Potter and the Chamber of Secrets - pp.133-4 - Bloomsbury - Chapter 10, The Rogue Bludger
He was also a symbol of hope for Dobby and his kind.
Because he considers Harry Potter to be wizard without equal (in temperament and disposition at least, though perhaps also skill since he notes that Harry has defeated Voldemort twice so far). There is clearly some sort of hero-worship going on here because of this. Since Dobby holds Harry in such reverence, he feels the need to warn him of the dangers despite the fact it runs counter to his orders (both implicit and explicit).
‘Sit down,’ said Harry politely, pointing at the bed.
To his horror, the elf burst into tears – very noisy tears.
‘S-sit down!’ he wailed. ‘Never … never ever …’
Harry thought he heard the voices downstairs falter.
‘I’m sorry,’ he whispered, ‘I didn’t mean to offend you or anything.’
‘Offend Dobby!’ choked the elf. ‘Dobby has never been asked to sit down by a wizard – like an equal –’
Harry, trying to say ‘Shh!’ and look comforting at the same time, ushered Dobby back onto the bed where he sat hiccoughing, looking like a large and very ugly doll. At last he managed to control himself, and sat with his great eyes fixed on Harry in an expression of watery adoration.
‘Harry Potter asks if he can help Dobby … Dobby has heard of your greatness, sir, but of your goodness, Dobby never knew …’
Harry, who was feeling distinctly hot in the face, said, ‘Whatever you’ve heard about my greatness is a load of rubbish. I’m not even top of my year at Hogwarts, that’s Hermione, she –’
But he stopped quickly, because thinking about Hermione was painful.
‘Harry Potter is humble and modest,’ said Dobby reverently, his orb-like eyes aglow. ‘Harry Potter speaks not of his triumph over He Who Must Not Be Named.’
Dobby leaned towards Harry, his eyes wide as headlamps.
‘Dobby heard tell,’ he said hoarsely, ‘that Harry Potter met the Dark Lord for a second time, just weeks ago … that Harry Potter escaped yet again.’
Harry nodded and Dobby’s eyes suddenly shone with tears.
‘Ah, sir,’ he gasped, dabbing his face with a corner of the grubby pillowcase he was wearing. ‘Harry Potter is valiant and bold! He has braved so many dangers already! But Dobby has come to protect Harry Potter, to warn him, even if he does have to shut his ears in the oven door later … Harry Potter must not go back to Hogwarts.’
Chamber of Secrets - Chapter 2: Dobby's Warning
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.
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".
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:
- think critically about the threat posed by the Diary;
- feel he - a house-elf - could do something about it;
- leave Malfoy Manor without permission;
- seek the attention of an unknown (and famous) wizard;
- tell Harry about the threat and by so doing betray his masters
- sabotage and hurt Harry afterward.
And the fact that he would punish himself for all this does not change anything. There are several passages that stress how much of an outlier Dobby is. Here are some:
“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.”
(Harry Potter and the Goblet of Fire, pages 108-109)
“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. [...]
(Harry Potter and the Goblet of Fire, page 417)
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.”
(Harry Potter and the Chamber of Secrets, page 57)
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.