You're in the middle of a game and your players have just wandered into a part of the map that you didn't finish prepping for/didn't expect them to get to. What do you do?This can obviously be extended into a broader question: when players do something unexpected that you as the DM have not prepared for, what do you do?
This happens a lot if you aspire to run any game where players have agency. Since that basically applies to all games I run, I encounter that situation a lot. My answer is simply that I improvise. It's something I think I am quite good at; I'm used to winging things, and what I "wing" usually works out well. I put this down largely to a combination of two things - practice/experience at the table, and also my job; I teach at a university, so, not to put too fine a point on it, I'm used to being put on the spot by awkward questions and scenarios and coming up with answers off the cuff. Of course, if it's a question about contract law my answer won't be pulled completely from my nether regions, but nonetheless, I'm having to think on my feet, in front of people, on a daily basis. This is a transferable skill.
This also means I barely ever use random tables to generate content on the fly. My brain does a decent job on its own.
However, that's not a particularly useful observation: I have a lot of tacit knowledge, but it isn't very easy to explain the process (I've written about this before). As is often the case, it's just a combination of practice and, probably natural flair. But so what? As advice goes "practice harder and hope you have natural flair" is as banal as it gets.
That said, I do think there is one important thing that any GM can do if they want to be able to come up with things on the fly: a strong sense of theme combined with a strongly developed sense of setting.
First, theme. By this, I don't, it goes without saying, mean 'plot'. Rather, I mean here that you as the GM should have a strong idea of what kind of game you are running and also what kind of game has emerged over time through the combination of your ideas and those of the players. If you are clear about this, whatever you come up with on the fly will be informed by it and fit in with it more-or-less seamlessly. If, thematically, the game is very concerned with social interaction and complex networks of interpersonal relationships, as a GM you should be consciously and explicitly aware of that fact, so that what you improvise on the fly fits with it and does not jar either you or the players. That should be obvious, and undoubtedly most GMs do this implicitly.
Secondly, setting. When, as a GM, you set up and prepare your game - the NPCs, the maps, the socio-cultural background, the random encounter tables, everything of that nature - if all of those things have a coherence and reflect a certain level of vision on your part, then your off-the-cuff creations will, again, be informed by it. How to come up with this 'vision'? There is no harm at all in using the 25 words or less approach. When I first started to come up with Yoon-Suin, I jotted down these 25 words to describe what the setting was all about:
Tibet, yak ghosts, ogre magi, mangroves, Nepal, Arabian Nights, Sorcery!, Bengal, invertebrates, topaz, squid men, slug people, opiates, slavery, human sacrifice, dark gods, malaise, magic.Everything has been based on, broadly, that ever since. Looking at it now, I might replace one or two of the words, but it still informs everything I create during prep/writing, and likewise informs the stuff I make up on the fly when I need to. This means that even when making things up as I go along when surprised by the actions of the players, I have a strong feel for the way the setting should be and that is like a thread holding everything together.