Another way of asking: where does the idea of a Dungeons & Dragons-style dungeon (an underground, windowless jail) come from?
I'm no expert in this field, but my understanding is that dungeons are far less common in the medieval period than entertainment media would have one believe.
For starters, the word "dungeon" comes from the French donjon originally meaning "keep", or the fortified tower constructed within the walls. Here is an example from The History of the County of Warwick, and referring to the construction of Warwick Castle.
Of Thomas Beauchamp the younger, Rous says only that he 'began the new towers next the dungeon'; here he is evidently using the word 'dungeon' in its original sense of 'keep' and is probably referring to the Watergate Tower at the foot of the mount, and perhaps also to a corresponding tower on the north side where a bastion was later visible.
There were underground prisons in some castles, but imprisonment was not a common punishment. As Foucault wrote in Discipline and Punish, there was a sharp shift in Western values around torture, punishment and imprisonment between the 16th and 19th century. Medieval punishments were typically public torture, disfigurement and/or execution. Imprisoning someone in a private chamber did not fulfill the need to demonstrate power.
Instead, those held as prisoners were often nobles awaiting payment of ransom. They would be kept in the most secure part of the castle, typically an above ground tower, or after the 12th century in a purpose built gatehouse chamber near the guard room to allow for observation.
The idea of dungeons is partially a relic of 16th century religious persecution and the use of torture, for example with Guy Fawkes. In addition, 19th century romantics imposed the idea of a dungeon on castles, mistaking storehouses and storage areas for large underground dungeons. The Tower of London was a notorious prison but didn't have a purpose built place to keep prisoners until 1687 when a brick shed "prison for soldiers" was constructed. Instead, most 15th and 16th century prisoners were kept in Beauchamp Tower where their inscriptions still mark the walls.
Some castles do appear to have had an oubliette or a small chamber accessible only through a hatch or hole in the ceiling. Warwick Castle has a terrifyingly small example that wouldn't allow one to turn around or lie down flat. But the word wasn't used in English until Water Scott's Ivanhoe in 1819, indicating it may be a fanciful notion of the neo-gothic revival.
--
Sources:
The Borough History of Warwick, 2008 Foucault, Discipline and Punish Hull, Understanding Castle Ruins in England and Wales