Dobby the house-elf first appears in Book 2, when he is still serving the Malfoy family. At the end of the novel, Harry manages to trick Lucius Malfoy into giving Dobby a sock: with the gift of clothes, Dobby is free. Dobby desperately admires Harry for his kindness, and is intensely loyal to Harry personally. He is also the only (happily) free elf we meet throughout the novels: he works for Hogwarts for a very small wage, and he also gets the occasional day off. Of course, since Dobby works for a salary and gets time off, many of the other Hogwarts elves consider him to be a bit crazy.
While Dobby doesn't pay a huge role in Book 5, he does make an interesting foil to Kreacher. Kreacher is an example of a house-elf who has been destroyed by a lifetime serving evil masters, the Blacks. By the time Book 5 rolls around, he deeply resents having to serve Sirius Black (who the rest of the family hated), but he has absolutely no choice. So Kreacher looks for ways to bend Sirius's orders to allow him the freedom to turn Sirius over to the Dark Lord.
By contrast, Dobby maintains the same fanatic loyalty to his preferred master that Kreacher does. The difference is that Dobby is allowed to choose who he is loyal to, and he chooses Harry Potter. Kreacher has had no choice, either with Mrs. Black or with Sirius Black. He may have loved Mrs. Black, but she also poisoned his mind and left him trapped on his own in an empty house for ten years. And he absolutely loathes Sirius, who treats him like a chore or an unfeeling object. By nature, Kreacher is loyal, but because he can't choose how to use that loyalty, he winds up crazy and miserable.
Dobby gets to spend time in the company of his favorite person; what is more, he is able to contribute actively to Harry's happiness. When the D.A. is looking for a secret place in the castle to meet away from Professor Umbridge's prying eyes, it is Dobby who suggests the Room of Requirement, a magical room that appears to people who have desperate need of it. Dobby is the one who tips off the D.A. that Professor Umbridge is on her way to find them, along with the Inquisitorial Squad. Dobby also spends a fair amount of time clearing the Gryffindor Common Room of the hats and socks Hermione has been knitting to free the house-elves. The other Hogwarts elves are so offended by these tokens of freedom that they refuse to enter the Gryffindor dorms, leaving poor Dobby to do all of the cleaning there. But he does it eagerly, because he enjoys working for the people he loves – which is, again, the direct opposite of poor Kreacher's dire straits.