Dobby came to the Dursley's house to warn Harry that he should not return to Hogwarts for his second year studies, saying he thought it would be too dangerous for Harry. Dobby admitted that he had been blocking Harry's mail; he had believed that if Harry got no letters from his friends, he would think his friends were false and would not want to go back to them.

Dobby bewitched one of the bludgers to repeatedly attack Harry in the Quidditch match against Slytherin; visiting Harry in the Hospital Wing that night, he admitted that he had hoped that it would injure Harry enough to cause him to be sent home. It is during this visit that he also explains how a house-elf can be freed, and admits that the Chamber had been opened before.

At the end of this book, Lucius Malfoy brings Dobby with him to the school, thus confirming that he is the house-elf of the Malfoy family, as Harry had believed. Dobby hints to Harry where Riddle's diary had come from; Harry confronts Lucius with this in front of Professor Dumbledore. Harry then tricks Lucius Malfoy into accidentally releasing Dobby from service to the Malfoys by presenting an article of clothing to Dobby, who promptly saves Harry from Mr Malfoy's wrath at having his servant freed.

Dobby spent at least a year searching for a family who would pay him wages to be their House-Elf, something which is unheard of in the wizarding world. Eventually, Professor Dumbledore agreed to pay Dobby one Galleon per week to work at Hogwarts – he had offered more, but Dobby bargained him down. When we see Dobby in the Hogwarts kitchens, he has just entered Dumbledore's service at the school, along with Winky.

At Christmas, Dobby wakes Harry up leaning over him in his bed; Dobby gives Harry a pair of socks; they are hand-knitted by Dobby himself. In return, Harry presents Dobby with a pair of socks – the ones which had been cushioning his pocket Sneakoscope for years. Ron also gives Dobby a pair of socks and a jumper (US: sweater). Dobby is quite overwhelmed by this.

Dobby happens to overhear Professor McGonagall and Professor Moody discussing the Second Task of the Triwizard Tournament, and discovers that Harry will need some Gillyweed to save "his Wheezy" (Ron Weasley); so he steals some from Professor Snape's office and gives it to Harry, waking him up where he had fallen asleep in the library mere minutes before the task is due to start.

Harry later visits the Hogwarts kitchens with Ron and Hermione, to thank Dobby with a gift of socks (Dobby's favorite garment) for his help in the second Task. Ron takes the opportunity to secure some additional food for Sirius Black, who at the time is living in a cave outside Hogsmeade.

We later learn that Professor Moody had deliberately let the information about Gillyweed slip when Dobby was in earshot, in hopes that Dobby would act on it.

When Professor Grubbly-Plank has healed Hedwig, Dobby brings her back to Harry. Harry is of course pleased to get Hedwig back, and speaks with Dobby for a while about how he and Winky are doing. Harry at this point is trying to find a place where the Defence Association, as it was then known, can practice, and he asks Dobby if he knows of any such place. Dobby, surprisingly, does, and tells Harry about the Room of Requirement, or the Come And Go Room. He has used it several times when he needed a place for Winky to dry out. He volunteers to show it to Harry, but Harry decides he is too tired, and it is too late at night; instead, he asks Dobby to tell him how to get in.

Dobby manages, somehow, to get into the room before the last meeting of Dumbledore's Army before Christmas, and decorates it with embarrassing Christmas decorations – baubles reading "Have a Very Harry Christmas", for instance. Harry does manage to get it largely cleaned up before the rest of the D. A. arrive.

Against direct orders from Professor Umbridge, Dobby makes his way into the Room of Requirement to warn the D.A. that Umbridge is coming. Because he has violated orders, he as usual feels a need to punish himself; he does this by charging headfirst into the wall. Harry does prevent him from injuring himself further. Harry is able to spread the word quickly enough that the only person actually caught is himself.

At one point, Harry feels the need to have Draco Malfoy trailed, to find out where he is going. He summons Kreacher, who appears entangled with Dobby – evidently they had been involved in a fight when Harry summoned Kreacher. When Kreacher calls Harry a "filthy friend of Mudbloods", Dobby punches him in the mouth and knocks his teeth out. Dobby volunteers to track Malfoy as well.

Shortly thereafter, Kreacher and Dobby return to report to Harry. Kreacher's report seems to be largely praise for Malfoy and his pure blood, and abuse for Harry, but Dobby is able to report that Malfoy had been spending time in the Room of Requirement.

When captured and in the cellars of Malfoy Manor, Harry sees a blue eye in the Magic Mirror he carries around with him, and asks it to send help. Shortly, Dobby apparates into the cell where Harry, Ron, Ollivander, Luna Lovegood, and Dean Thomas are being held. Dobby is apparently extremely nervous at being in the Malfoy home, where he was so cruelly treated before Harry won him his freedom. Harry asks him to take Luna, Dean, and Ollivander to Shell Cottage. The noise of Dobby Disapparating attracts attention from upstairs; Wormtail is sent down to the cell to investigate, and Ron and Harry attempt to overpower him. Harry calls on the life debt that Wormtail owes him, and he hesitates; the silver hand that Lord Voldemort had given Wormtail senses this hesitation, and reading it as disloyalty, chokes Wormtail to death.

Harry and Ron proceed upstairs to try and save Hermione and Griphook. Though disarmed, Bellatrix Lestrange produces a silver knife and threatens Hermione with it; she has Draco collect all the wands Harry and Ron are holding. Dobby re-appears and causes the chandelier to fall on Bellatrix; gathering all the wands Draco is holding, Harry takes Griphook and Dobby, and Ron takes Hermione, and together they Apparate to Shell Cottage. As Harry is Disapparating, however, he sees Bellatrix throwing her knife; arriving at Shell Cottage, he finds that the thrown knife has hit Dobby, killing him.

Harry then digs a grave for Dobby, and buries him, near a tree on the grounds of Shell Cottage.

Later, Aberforth Dumbledore asks where Dobby was, saying that he had expected Dobby to be traveling with Harry; Harry tells him that he was killed by Bellatrix. Aberforth is dismayed; he says he "liked that elf."

Dobby is very loyal to the people he cares about. He did what the Malfoys told him to do because he had no choice. Despite the loyalty he owes to the Malfoy family, however, Dobby still tries unsuccessfully in the second book to protect Harry. He later helps Harry in the Second Task in Goblet of Fire, tells him about the Room of Requirement, warns the DA, and tails Draco Malfoy for him. He is also loyal to Dumbledore who employs him, and helps his friend Winky when she is drunk. He is also a hard worker. Dobby is also a very grateful person. In general, he is a good friend to have around.

Like all house-elves, certain subtleties of human behavior are beyond his understanding; his speech pattern, for instance is somewhat immature: he will speak in the third person, apparently unable to master the subtleties of "I" and "you". He is completely subservient, seeming to exist only to take orders from one wizard or another. He does have a will of his own, reflected in his being able to try and warn Harry away from Hogwarts against his owner's wishes, but he has to make a supreme effort to allow that will to express itself, and because he is going against the wishes of his owner, he must punish himself. He has internalized his subservience so much that he will punish himself without being told to, if he finds himself doing something which he believes his owners might not like.

Dobby, being emotionally very simple, in some ways childlike, forms relationships very quickly, albeit at a possibly child-like level. Initially, of course, he is required to be loyal to the Malfoy family, but even within the constraints of that loyalty, he attempts to protect Harry, who he sees as something of a saviour, from efforts to harm him. Dobby says that this is because of Harry's defeating Lord Voldemort; house-elves were really very badly treated when he was around. "Of course, Dobby is still treated very badly...", he goes on to say; but Harry is respected because of what he did for the general House-Elf population. Once Harry engineers his freedom from the Malfoys, Dobby must hunt for some other Master to serve, eventually setting on Hogwarts and Dumbledore; while his loyalty then is to the school, he clearly is also loyal to, and protective of, Harry, even attempting to defend Harry from slurs by Kreacher. Additionally, he takes care of Winky, who he apparently feels responsible for, having found her the job at Hogwarts. To a lesser extent, Dobby also comes to admire Ron when he receives one of Ron's Christmas sweaters.

It is possible that Rowling has used the relationship between Harry and Dobby to send a message to the reader, teaching equality of all and using the difference in species to comment on racism. We are repeatedly told, by one wizard after another, that House-Elves are happy with their lot, that they exist to serve, and that granting them freedom would be an injustice; but Dobby, perfectly happy to be a Free Elf, shows that this generalization is, like all such, an over-simplification. However, by showing how the relationship between Harry and Dobby continues, and Dobby's essentially child-like nature, we can see how wizards in general could perpetuate this discrimination.

By the same token, we should note that Dobby's pleasure at being freed largely stems from the way he was maltreated by his owners, the Malfoy family. Dobby's treatment at the Malfoy's hands contrasts with the treatment of the Hogwarts house elves; while still subservient, the Hogwarts house elves are treated as individuals with worth, and are not abused or belittled. The Malfoys' treatment of Dobby displays how this sort of inequality can be abused.

It is also worth mentioning that Dobby's apparent pleasure at his freedom is one of the main things that supports Hermione's attempts to win freedom for house elves. None of the other Hogwarts house-elves seems to want freedom, to the point of refusing to clean Gryffindor tower because of the elf hats Hermione knits and leaves lying around. Dobby, as a Free Elf and already clothed, thus ends up cleaning the Gryffindor dormitory single-handed. Quite possibly, it is the difference in how they are treated that determines whether freedom is seen as a good thing or not, and it is only the very worst treatment that results in an elf being willing to risk the uncertainty of freedom.