The word Dungeon actually originates from an Old French term for castle, but over the centuries has evolved to mean something quite different. A Dungeon in the modern sense is a medieval prison, generally kept underground in the basement and a typical part of many castles. Generally a dungeon is a room with a heavy wooden (often oak) door, small barred windows (if any), some latches for shackles and a bucket for dealing with human waste, or a small number of said rooms. Optional additions included a bench, reeds and maybe a cheap straw mattress if the owner of the castle was particularly indulgent.
As a rule people did not stay in a dungeon for long in the middle ages. The penalties of medieval justice were either to the effect of fines, community service and public humiliation for mild offenses or public floggings, mutilation and death in various levels of gruesome painfulness for more severe ones (and a generally broader sense of what constituted a "severe crime"). Either way these penalties were immediate and quick in their implementation, or at least generated or saved some revenue. Housing people under lock and key was expensive as you needed to feed them and keep them under guard back in the day when people were literally willing to work for food and a roof over their heads. Dungeons were used mainly to store people temporarily until the real punishment happened, to house captured enemy soldiers being ransomed or individuals who were to be interrogated. At most you might toss a servant into the dungeon for a night for some minor offense if they were otherwise vacant. Mass Incarceration only really caught on in the early modern period.
Dungeons (well, structures loosely inspired by dungeons) are also a major part of Dungeons & Dragons. In the context of D&D (and many other role-playing games, including online ones), a dungeon is a structure (typically underground, featuring cavernous rooms connected by twisty passages and corridors) inhabited by monsters and traps and containing loot. The basic hack-and-slash game features player characters delving into dungeons, fighting monsters, and disarming traps, all to get to the treasure chest at the end of the hallway. Who built the dungeon? Why did they think the treasure was important enough to hide in it? How do those monsters stay alive in between making meals of adventurers? These questions are often left unanswered. The profusion of the dungeon as the archetypal adventuring locale has somewhat blurred the definition of what a 'true' dungeon really is. The idea of "dungeon-crawling", that is a long mission through a dungeon for the purpose of defeating enemies and gaining loot, is today applied to basically any combat mission in an RPG where the party operates with minimal support in an enclosed area filled with hazards, all for the promise of loot.
How to Dungeon
If you play in a group of fairly stab-happy people who care only for the three moments of "bust in door", "kill shit" and "loot the bodies", how a dungeon looks may not be so important, but to the rest, a dungeon has to make sense. Since we don't really have large caverns and ruined castles filled with monsters in the real world, it can seem strange and unrealistic to delve into one in a tabletop game. There's a few things one can do to make them feel more fit for the setting:
First, make sure the dungeon's general layout serves or served a purpose. Any old maze will feel weird if we're told the dungeon is an old, decrepit fortress - a fortress is laid out in certain ways that makes them easy to defend, so reflect that. If you wanna mix things up feel free to add obstacles, cave-ins and broken walls but for Pelor's sake, don't just map a bunch of random rooms and paths. Dungeon delving is also story-telling through showing. At best, the players can even make logical assumptions based on your design - If it's a fortress dungeon, there's got to be a armory filled with weapons somewhere, right? Players love to be rewarded for making good leaps of logics, so make sure they can do so comfortably through your dungeon design.
Second, there are monsters in dungeons, but not random ones. Whatever critters you wanna fill your death-maze with, make sure to have some background for them. Is that Owlbear the players just encountered looking for food in the cavern, or does it live there? What about the goblin tribe back some rooms; where they hiding from the Owlbear - and so on and so forth. Again, dungeons tell stories. What makes that particular dungeon a great place to hide out in for the monsters? You can even indicate this to the players by seeding plot hooks about it - the local town ousted a cult, who fled eastward to the old, abandoned manor on the hill, or maybe tracks of boots are spotted going back to the glacier where the next dungeon hides? There's a bunch of options.
Same with traps and puzzles - don't add a poison spike trap in a dank swamp cavern if there's no one in the cavern that could or would make one. If your players delve into a lost bandit hideout, traps are unlikely, but a kobold nest will be rife with traps of the nastiest kind. Traps doesn't have to be created either - old, unstable floors that break underfoot or bubbling pools of acid can serve as just as much a challenge than any trap you found in your Dungeon Master's Guide. Remember Moria of LoTR fame; it was treacherous mostly because it was falling apart and because any noise would attract goblins in droves of thousands.
So, basically always remember the How and the Why of your dungeon - How was it created and Why is created that way? How did your monsters find their way into the dungeon, and why?
"Dungeons" have a long history in fantasy literature predating Dungeons & Dragons. Examples include:
- Stardock, both a mountain and a treasure-and-creature filled crag featured in Fritz Leiber's "Fafhrd & Grey Mouser" stories
- Quarmall, a whole underground kingdom complete with civil-war-waging mage-priest-kings, ibid.
- Dol Guldur, the dreaded "Hill of Sorcery", home of "The Necromancer" (another name given to Sauron in The Hobbit); it was here that Gandalf met the half-mad grandfather of Thorin Oakenshield and retrieved the map and key to The Lonely Mountain. Courtesy J.R.R. Tolkien.
- Moria, underworld kingdom-fortress of the dwarves and one of its wealthiest mines until overthrown by a primordial demon, ibid.
- Angband, stronghold of Morgoth far in the frozen North and home to thousands of slaves and vile creatures, ditto.
- Lloyd Alexander's Chronicles of Prydain featured a proper dungeon beneath "Spiral Castle", which contained a sword capable of defeating Lord Arawn, one of the chief bad guys in that series.
- Fafnir's lair, the archetypical lair (or at least one of the earliest examples of one in written text) of an evil dragon that is filled with gold and other valuables from the Nibelungen Saga of German and Nordic origins.
All of these are, to a greater or lesser extent, halls and galleries and caverns, rooms and mazes, located exclusively underground, loaded with monsters, maps and treasures.