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. 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 painfulness for more severe ones. Either way these penalties were immediate and quick in their implementation, or at least save the government some money through convict labor. Housing people 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.
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.
"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.
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.