In the book, Harry Potter and the Deathly Hallows, Dobby disarmed Bellatrix Lestrange, who was trying to kill Harry. This allowed Harry to disarm Lucius Malfoy, who then lost control of his wand and accidentally killed Bellatrix. Dobby was fatally wounded by a curse from Bellatrix, but he died a hero.
This scene is portrayed in the movie adaptation as well, with Dobby taking the curse intended for Harry and dying in his arms.