You see, Dobby did not actually die, but he made everyone think he did. First of all, the reason for this is that he knew that it was important for the death eaters to think that one of the enemy’s most valuable assets was destroyed, because then they wouldn’t see him coming. The reason Harry, Ron, and Hermionie had to believe it as well was because he knew it would fill them with both remorse, and RAGE. They could use this rage against Bellatrix Lestrange, who was one of the death eaters most valuable assets, and the one who “killed” him. Dobby actually did show up at the battle of Hogwarts, he was the one who apparated into Dumbledore’s study to unlock the door to allow Harry to enter. He couldn’t reveal himself before Harry “died” since Bellatrix was still not defeated. However, he was captured by Bellatrix during the battle and was imprisoned, and was trapped in an enchanted cage for years until the magic wore off, allowing Dobby to apparate out. (Bellatrix didn’t kill him because she died)
Also, the way Dobby escaped was by creating a false projection of himself to fool everyone.