You don't have to do any of that, though. You can run in the moment and let the play direct the game. And, this isn't as hard as it's made out to be, nor does it generate less deep play. It's only hard if you bring pre-writing assumptions with you. If you keep everything not already established in play as fluid, and only generate what's needed to continue the direction of play, then the 'making it up on the fly' is actually pretty tightly constrained and follows naturally from the events in play.
Now, games that don't give the GM levers make it harder. By this, I mean games that present pass/fail checks without grades of success or failure (or both at the same time) make following the fiction a tad harder, but that can be addressed by moving the point of focus. What I mean by this is that games, like 5e for instance, that do pass fail also tend to have a focus on the immediate action. The game generates obstacles like a locked door who's resolution is to unlock the door via a skill check, and that's either passed or failed. If passed, you move to the next atomic obstacle and repeat. If failed, you repeat the check or do another check, or abandon the obstacle, but that obstacle is the focus. This is enormously hard to ad lib, because it feels, on the GM side, like arbitrary roadblocks with no where to go if you faceplant a few checks -- you have to ad lib a brand new direction after you just did that for this one! Yikes, scary, hard, not rewarding. But, this is how you build these games from prep, where you have the time to consider other routes. You can't do this when running in the moment. You have to change focus to the bigger objective. In this example, the objective may be to get into the castle. You then just have to present a number of obstacles to this -- maybe 3 or 4 -- of which a locked door could be one. Then, on a success, you advance, on a failure, you add a complication. You don't need to have guard routes pre-planned or look at your notes, a failed lockpicking results in a guard patrol (or other thing, whatever fits). By putting the mechanics to work, you don't have to make everything up, or ad lib a complete castle, you just need to to the parts that are needed when they are needed. It's far easier than assumed, if you actually let go of all of the assumptions of prep and the idea that you, as GM, have to present a world that is previously defined for the players to interact with.
Now, if you use maps and minis, this does get harder. 5e isn't super easy to have the tactical wargame part mix in with ad libbing. You have to make choices, which goes to my larger point that most of the 'hard' work of 'harder than the players' work in D&D is a choice. You don't have to work that hard to run D&D. You choose to.