(Click the character infographic to download.)
Harry Potter and the Chamber of Secrets presents two different kinds of prejudice. One is anti-Muggle-born bigotry, as when Draco Malfoy calls Hermione a Mudblood. The second prejudice that influences the plot of Chamber of Secrets is wizarding bigotry against magical creatures.
Dobby is the first magical creature we've really gotten to know in the Harry Potter series. Dobby is suffering from terrible abuse at the hands of a Dark family that he serves (the Malfoys). Still, Dobby has no means to free himself from torment; the only way a house-elf can be freed is if his master gives him a gift of clothes. The injustice of Dobby's position gets addressed at length with other house-elves in Books 4 and 5. The important point is that we have further evidence that the wizarding world has the same social problems our world has.
Dobby focuses his efforts on Harry for a specific reason:
If [Harry Potter] knew what he means to us, to the lowly, the enslaved, we 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 […] But mostly, sir, life has improved for my kind since you triumphed over He-Who-Must-Not-Be-Named. (10.144)
For Dobby, Harry represents justice. By defeating Voldemort, Harry improved lives for creatures battling the prejudice and bigotry of the pureblood wizards gathering with Voldemort. By protecting Harry, Dobby is also trying to protect a future for his own kind. Of course, Dobby is just a tiny bit heavy-handed with this protection, what with stopping Harry's letters from his friends over the summer, closing the magical barrier at Platform 9 3/4 for the Hogwarts Express, and finally enchanting a Bludger to injure Harry during his Quidditch game with Slytherin. Still, despite all the damage he does, Dobby obviously means well.
Finally, Dobby gets his reward. Indeed, the last chapter is called, "Dobby's Reward." When Lucius Malfoy arrives at Hogwarts to chew out Professor Dumbledore for returning as Headmaster to the school, his house-elf accompanies him. When Harry hands Lucius Riddle's damaged diary wrapped in a smelly old sock, Lucius throws the sock to the floor – and Dobby catches it. So Harry manages to trick Lucius into freeing Dobby from his slavery. That's Harry Potter, working against injustice one house-elf at a time. Dobby is an odd, funny character, but his presence in the novel highlights an aspect of wizard prejudice that will become hugely important from Book 4 on: anti-magical creature suspicion.