(Click the character infographic to download.)
Dobby is a house-elf who turns up in the Hogwarts kitchens in Goblet of Fire after being freed (accidentally) by the Malfoy family (with the help of Harry). Dobby was Harry's first house-elf friend: in Harry Potter and the Chamber of Secrets, he's the one who tells Harry that Lucius Malfoy planted Tom Riddle's diary on Ginny Weasley.
Dobby has spent the intervening year between Books 2 and 4 looking for work. But it's tough to find a job as a house-elf if you want days off and payment. These things aren't really part of the usual house-elf gig (no matter how much Hermione wants them to be). Luckily for Dobby, Professor Dumbledore is willing to hire this oddball among house-elves:
"Professor Dumbledore offered Dobby ten Galleons a week, and weekends off," said Dobby, suddenly giving a little shiver, as though the prospect of so much leisure and riches were frightening, "but Dobby beat him down, miss ... Dobby likes freedom, miss, but he isn't wanting too much, miss, he likes work better." (21.140)
So Dobby has learned to want better treatment at the hands of his wizard employers. But he's still a house-elf, with a house-elf's nature. He likes work better than time off. Dobby is also still intensely loyal to his wizarding masters. He seems to have adopted Harry as his best wizard friend (though he sure loves Ron and Professor Dumbledore, too). Since Dobby adores Harry, he brings Harry news of how he can get through the second task, which deals with the merpeople under the lake:
Dobby hears things, sir, he is a house-elf, he goes all over the castle as he lights the fires and mops the floors. Dobby heard Professor McGonagall and Professor Moody in the staffroom, talking about the next task ... Dobby cannot let Harry Potter lose his Wheezy! (26.103)
(Harry's "Wheezy" is Ron Weasley, the thing Harry will "sorely miss" (25.53). Hermione is Viktor Krum's dearest person, Cho Chang is Cedric's, and Gabrielle Delacour is Fleur's.) So Dobby steals gillyweed from Professor Snape's private stores to allow Harry to breathe underwater during the second task. (Later on, we find out that Moody engineered all of this in order to make sure Harry wins the second task. Dobby, however, has no idea of any ulterior motives on the part of the Hogwarts professors.)
To thank Dobby for his help with the second task, Harry buys Dobby stacks and stacks of Dobby's favorite thing: socks. After all, it was a misplaced sock that bought Dobby his freedom from the Malfoys. We think we can understand his enthusiasm for them.