p.marino's answer covered how to include a megadungeon in a setting fairly well, so I'll talk a bit about how to make a megadungeon a larger part of your setting, maybe even the entire setting.
More specifically, I'll focus on questions you should ask yourself about your megadungeon to help you build the story around it. Once you've decided on the answers for most of these questions, you'll have the general concept of both your megadungeon and your story planned out.
Is There an Outside?
Is the megadungeon the entire plane, or just part of it? If it's the entire plane, is it infinite, or just really really big? If it's not the entire plane, how & why did the PCs enter it? By choice, were they forced in, tricked into entering, or just woke up in there with no idea how/why? Or were they born there?
What kind of entrances/exits does it have? Does it use teleportation or do you just walk through a hole in one edge? Are any of the access methods one-way, due to teleportation, extremely long drops, guards, etc? How much traffic goes through these access points, both now and historically? Did there use to be a lot more traffic but now outsiders are rare, or have new entrances suddenly opened, dumping lots of new immigrants into the megadungeon?
If there is no "outside" the megadungeon (besides other planes) or if it's largely self-contained (yes, it is possible to make a megadungeon that's not self-contained, it's just hard, and tends to involve an interconnected network of flying islands or something equally elaborate/contrived), how does its ecology work? In the real world, plants get energy from sunlight, herbivores get energy from plants, carnivores get energy from herbivores, and fungi and other such things get energy from breaking down dead plants/herbivores/carnivores. Where does energy enter your megadungeon? You can always hand-wave this one away by saying, "it's a fantasy setting, the energy comes from naturally-occurring magic".
Where Did It Come From?
Was your megadungeon created by someone, such as a god, multiple gods, Far Realm entities, etc? Did a wizard do it? Did it form on its own? If so, did it happen naturally over time (and is it still growing?) or was its creation triggered by something? If it was triggered by something, what was the catalyst? Did it grow around some powerful artifact? You don't necessarily need to answer this question; if the megadungeon is merely where the story happens rather than a focal point of the story, then it doesn't really matter where it came from. Maybe nobody knows.
How Does the Megadungeon Connect to the Party's Goals?
Are they trying to escape it? Trying to figure out where it came from? Trying to protect it from corruption from within? Trying to protect it from external attack? Trying to prevent it from corrupting the outside? Trying to figure out why they woke up there instead of the inn they went to sleep in, and how to get home? Or is it merely where they happen to be going through a more standard political/intrigue/heroism/mercenary/collect-the-MacGuffins/kill-the-BBEG story?
Who Lives There?
Are there entire cities down there, or just scattered outposts? Cities tend to be pretty apathetic about new visitors, since you need tons of them to become a city. Scattered outposts, on the other hand, tend to feel strongly about newcomers, usually based on whether they've had bad experiences in the past; if they need every sentient they can find to help fight off ravenous monsters, they may welcome you with open arms, whereas if they're doing OK on their own they probably don't take kindly to people they don't know since they might be raiders/thieves/etc.
What kinds of races are found down there? What races occur primarily outside, and so are uncommon in the megadungeon? Did any races migrate en masse (voluntarily, by force, by hostile magic, or by natural disaster) into the megadungeon? Have any races evolved or been created there? The more races that entered the megadungeon in large groups or came into existence there, the more racially homogenous population centers will tend to be. If most of the racial groups entered in little packets, population centers are likely to be very multi-racial and cosmopolitan, even if immigration from outside has drastically decreased and most population increases now comes from childbirth.
What Are the Different Regions?
A megadungeon should never be homogenous; mile upon mile of identical caves gets boring in a hurry. A lost dwarven city, natural caves, the hive of giant insects, and the magic-carved laboratory of an ancient wizard can all be underground and have walls of stone, but they'll still have very different layouts and feel very different to the party (as long as they're described properly).
Each of the above questions can (and should) be applied to each region of the megadungeon. How does it connect to other regions? How did it form and/or why is it the way it is (why isn't it just more of the next region over)? Who (and/or what) lives there? What population centers are there, and how do they feel about other people (both wanderers in general and their various neighbors)?