There is a ancient dungeon full of treasure. The world is full of powerful barons with military, temples with hardy clerics and paladins, cunning wizards with apprentices. The logical question is: why did they not empty out the dungeon, slowly and systematically, long ago?CC-BY 4.0.
The true answer of course is, because then there would not be an adventure to be played. But, we want also an in-game answer, some contrieved logic that supports this unnatural state of things. Here are some options for possible explanations, and how good they are:
1. The dungeon is hard or dangerous to reach. Cloud castle, under the sea, icy mountains, desert, disease-ridden bog, monster infested forest, deep in enemy territory, etc. The cost, effort and logistics to get there and to get treasure back to civilisation from there may make plundering it a questionable undertaking. (And turn the trip into an adventure in and of itself, at least for low-level parties).
2. Access to the dungeon is controlled by the local authorities or community, who want the dungeon to remain unraided, or meter access - for example to attract affluent adventurers, to extract taxes on the treasure secured, due to taboos, sacred place, worried that something horrible is going to be awoken that better stay asleep etc.
These first two arguments could keep away normal would-be grave-robbers. A powerful group of adventurers can ignore authorities and has magic to overcome these obstacles. The question remains: Why has no such group gutted it yet? The hostile territory argument in particular only works, if the forces that control it are not organized themselves, because else they could have organized raiding it for their own benefit long ago.
3. The dungeon has a reputation for deadliness. People and even organizations are afraid to enter it. This can work for high-level killer dungeons like Tomb of Horrors. A dungeon harboring a gate to hell that cannot be destroyed, infested with devils, may best be left undisturbed. However, a dungeon that is written for a first level adventuring party does not make this narrative credible. A few giant rats and kobolds would not do against the experienced adventuring parties or authorities that exist in the game world, and at least the upper levels would be cleared already. This also does not work for classical megadungeons, where the upper levels are not that dangerous, even if the deeper levels are.
4. The dungeon just recently became known or accessible. It has been rediscovered, or it used to be magically protected and only recently did these protections lapse. In this case now the race is on for plundering it, and if the characters do not engage with urgency, it indeed will eventually be emptied out by competing groups. This is a useful device to create time-pressure. You could also imagine groups of adventurers all waiting for the gate to open, each wanting to be the first one to strike it rich (and then of course, some of them being redshirted by some horrific opening encounters).
5. The dungeon, its location or its entrance is hidden or unknown. Nobody even knows about it or how to get to it, other than its inhabitants, who prefer it stays this way. This only works if no monsters issue from it causing problems, so there is no reason for authorities to do anything about it. If the dungeon is entirely forgotten, even extremely powerful groups have no reason to pursue it. If only the location or entrance is forgotten, this might not stop high level parties with access to lots of divination magic.
6. Access to the dungeon is magically gated in a way that cannot easily be bypassed. Cursed fog, magical gates that only open for someone who has the key, access opens only at a certain time and for a limited duration, dungeon cannot be found if searched for, etc. Magical wards must be from power beyond the reach of high-level characters, or spells would have overcome them. A locked gate that can be defeated with dispel magic or dimension door is of limited use as a deterrent. This still might be an explanation, if there are only few high-level groups around, and too many old tombs that nobody knows about for them to address all of them.
7. The Dungeon is home to an entity of god-like power. That entity keeps it around. This is the solution easy access megadungeons like Greyhawk Castle and Undermountain chose, to explain why upper levels that can be explored by beginners and pose no challenge to experienced adventureres have not long been cleared. Both use the construct of a demi-god like wizard in the foundation of the dungeon who is re-stocking the dungeon for his entertainment. The reason that the dungeon has not been emptied out is because it is being refilled. This is especially convenient, as such a whimsical being can be used justify all the other illogical and hard-to explain things that go on in the dungeon.