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 shield of reflection. 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). 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, potions of extra healing, or potions of full healing.
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.
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 carry in a sack and use as petrification grenades. 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. Note that, if Demogorgon is standing next to you, he will follow you to another level, even if you use a mechanism (such as cursed potion of gain level) that would not be available to him. You must get two squares or more away from him before attempting to escape to another level. You can do this by teleporting him, paralyzing him, being very swift, 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 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!
Another way to kill Demogorgon is quite simply to be a Knight, be very fast, be on horseback, be an expert with the lance and use it instead of Excalibur or any other relic sword. At Expert level a Knight has 80% chances of jousting and this will stun Demogorgon. A stunned Demogorgon will be unable to teleport and the very fast Knight will be able to finish him before Demogorgon is able to retaliate. Also Demogorgon is one of those rare superheavy monsters who don't recoil one square when they are jousted, so the Knight doesn't need to spend time moving adjacent again after a successful joust. The sequence of the fight is, Demogorgon teleports adjacent, stings the Knight, Knight jousts Demogorgon, Knight attacks, Knights attacks, Knight attacks, and so on until Demogorgon is dead. Then Knight applies his blessed unicorn horn to get cured. Because he will not have Excalibur in hand, the Knight needs a cloak that confers MC3 to avoid level drainage. Another option for the Knight is, after the initial joust, move one square away and resort to pounding, this has the advantage of not risking that by pure bad luck the stunned Demogorgon attacking in the Knight's direction, however it is longer and in addition as Demogorgon is not being restunned again and again as in first method, there is chance he can recover and teleport away. If Demogorgon lands a second disease attack in the middle of this process, or if you are getting close to twenty turns spent "Ill", you will need to break off long enough to get well, then try again.
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.