Demogorgon is the most difficult of the demon princes and, indeed, the most difficult monster of any sort to appear in NetHack. Fortunately, his appearance is not guaranteed and can only occur through demon summoning. He can be summoned directly by Orcus, Juiblex, or Yeenoghu, indirectly by any demon that can summon these, or (rarely) by any spellcasting monster that casts "summon nasties" in Gehennom, which includes the Wizard of Yendor.
The safest approach to Demogorgon is not to meet him in the first place. As mentioned above, in vanilla NetHack, Demogorgon will not appear unless summoned, and Orcus, Juiblex and Yeenoghu are the most likely creatures to do this. Therefore, when facing any of these foes, kill them as expeditiously as possible. If you get bogged down fighting lesser threats, you are giving Demogorgon an opportunity to arrive every turn.
Conversely, if you want to meet Demogorgon, perhaps as the ultimate demonstration of your demon-hunting prowess, then let Orcus spend all the time he wants bouncing death rays off your Silver dragon scale mail. Demogorgon will be along eventually.
The only way to survive Demogorgon is to be prepared. Just as you carry an escape item for emergencies, work out your "Demogorgon Kit" and carry it in inventory when you enter Gehennom. Be very fast and unburdened, capable of curing diseases, and be able to both attack him and (probably) escape from him. You will also need speed boots or haste self, and either a unicorn horn (preferably blessed) or the Staff of Aesculapius as equipment. A safe, one-turn way of engraving Elbereth is vital (fingers in the dust are not safe) if you can engrave it without being stunned. If both of his attacks succeed he will stun you and engraving will likely fail, so a scroll of scare monster in a container is very helpful. If Yeenoghu summoned him, you may have a hard time to be neither confused nor stunned for a turn. If you managed to drop the scroll and afterwards cured your ailments, you should engrave Elbereth immediately, for the case that a random fire attack (hell hound for example) destroys it (rays that miss you are not affected by reflection). Unless you know you will stay on Elbereth only, you need magic resistance. Be aware that not even a blessed unicorn horn is perfectly reliable for curing sickness—the safest items are holy water, blessed potions of healing, extra healing, or full healing, or the spell at 0% failure rate.
You should read the entry on his disease attack and understand the implications: his first attack will leave you with between 20 and (19 + constitution) turns to live, which is a fairly comfortable margin for healing yourself, but each subsequent disease attack he lands will reduce your remaining turns of life by roughly two-thirds. There is no way to know the exact value of your "turns to live" counter if you are diseased, so you should act as if the worst case were true in every circumstance. As noted elsewhere, if you have a short, safe way to engrave Elbereth, Demogorgon will stop physically attacking (and therefore sickening) you while you remain on that square. Note however that as of version 3.6, Elbereth is no longer effective in Gehennom, and even an engraved Elbereth has a chance of eroding.
As for how to actually kill him, there are several ways. First, you can beat him to death, but this is very slow and difficult to do. If you are not on the up stairs yourself, he will teleport there to heal when you damage him, and, if he needs to, he will use them to escape the level. He is not immune to stoning or disintegration, so you can kill him in that manner; if you have any wishes left, wishing for a cockatrice corpse could be a really good idea. Kate Nepveu, author of the Demogorgon FAQ reports killing Demogorgon by polymorphing into a cockatrice and allowing him to sting, but adds that this is a risky gambit and she "can't really recommend it." More reliable is polymorphing into a female cockatrice ahead of time, and laying a few eggs that you can use as petrification grenades (leave them in a container when they would hatch to prevent that). If you have a non-cursed scroll of earth and a ranged attack other than striking, you can keep him at a distance.
Finally, if you can, GET AWAY! Leave immediately and get past him. Teleport to the staircase, zap a wand of digging downwards, quaff a cursed potion of gain level. Use the breathing space to reassess, re-equip, and move back in on your own terms. Discretion is the better part of valor. Demogorgon will follow you to another level if standing next to you, even if you use a mechanism (such as cursed potion of gain level) that would not be available to him. You must not be adjacent to him before attempting to escape to another level. You can do this by teleporting him, paralyzing him, being fast enough, damaging him so he teleports, etc.
Engraving on the Up-stairs
Many strategies for tackling Demogorgon involve getting to an up-stairs before he can, since this is the only location Demogorgon will teleport to when he flees you to lick his wounds. If you are occupying this square, he has nowhere to teleport to, and he can't escape upstairs with you blocking the path. If you can reach the up-stairs first and permanently engrave Elbereth there, you can beat on him freely with no risk of illness (he will keep moving next to you to cast spells, but will not strike at you).
There are, of course, several ways of doing this. If you are not on a no-teleport level, you can teleport to the stairs. If you can escape one level up without Demogorgon pursuing you (see above), you can go to the downstairs of the higher level and simply walk down. If you can get to the downstairs on the current level and wait for Demogorgon to get next to you, you can walk down to the next level and let him pursue you. Or, perhaps you can paralyze Demogorgon (with a wielded or thrown potion of paralysis) and run there before he wakes up. If your plan involves walking down stairs, there is a risk that Demogorgon, and not you, will end up standing on the actual staircase square, so you must plan for this contingency (teleport him away, for instance, or damage him so he flees upstairs and try again). Above all, pay attention to the turn counter and get well if you are diseased!
If you are a Knight, are riding, and have a lance and expert skill in it, you may be able to joust Demogorgon to death before he gets a chance to teleport to the stairs (if you're lucky you'll even be able to hit him before he sickens you). If he does get away, you can just use a unicorn horn and try again.
In SLASH'EM, Demogorgon has his own lair somewhere between second and sixth level of Gehennom, as well as Juiblex, Yeenoghu and Orcus – so you will meet him, it's guaranteed. Note that Demogorgon's lair is a non-teleport level – don't even try to use your wand of teleportation. Demogorgon still can teleport to the staircase, and to you, as usual for a covetous monster.
Keep in mind, that the behavior of unicorn horns has changed in SLASH'EM, making them far less reliable. However Enchantment now matters a lot. You should come prepared with a highly enchanted horn to deal with the sickening attack.
In UnNetHack Demogorgon does NOT respect Elbereth. He sometimes appears in his own lair in Gehennom, but this is not guaranteed; the lair takes the form of a small self-contained maze surrounded by a moat of ice.
In Slash'EM Extended Demogorgon also does NOT respect Elbereth just like in UnNetHack, and is immune to stoning, so the usual strategy of throwing a cockatrice egg won't work either. He will always be waiting in his lair in the Gehennom; all chaotic major demons can randomly summon him on other dungeon levels, though.
He comes from Dungeons and Dragons. D&D in turn derived him from an invented pagan god, by Christian scholars. In both sources, he is a powerful demon.
According to D&D: He is 5.5 meters/18 feet tall and has a vaguely humanoid/reptilian body, with two mandrill (like baboons) heads, two long reptilian necks and two tentacles for arms.
His image graces the front cover of 4th edition D&D's Monster Manual 2.
A terrible deity, whose very name was capable of producing the
most horrible effects. He is first mentioned by the 4th-century
Christian writer, Lactantius, who in doing so broke with the
superstition that the very reference to Demogorgon by name
brought death and disaster.
[ Brewer's Concise Dictionary of Phrase and Fable ]
Demogorgon, the prince of demons, wallows in filth and can
spread a quickly fatal illness to his victims while rending
them. He is a mighty spellcaster, and he can drain the life
of mortals with a touch of his tail.
- The excellent Demogorgon FAQ by Kate Nepveu.