Planning is a big part of being a Dungeon Master. However, sooner or later our best laid plans will be left behind. Our planned scenario could have gone by faster than we anticipated, it could have been that players did something completely unexpected, or a bunch of other reasons. However, when it does happen the Dungeon Master will be need to improvise. Such a thing, particularly if you aren't used to it, can be challenging. It is for that reason I hope to give some advice. If you have your own, I'd love to hear it too.
Constraints Can Be Good
The first I want to say is that some constraints can be a good thing, particularly if you don't have much time to think of something. Some of the techniques I'll be mentioning do just that. They help narrow things down, which is one of the major issues when improving. When you can do anything, how do you decide what to do? If you are going into a campaign, there will be some implied constraints you'll end up putting on yourself. Your fight against a necromancer will probably features undead and necromancers. If you need to come up with an encounter, it'll probably involve one of the two. Of course, this situation is a bit awkward since almost anything can be undead, but still it narrows things down a bit.
Making Characters on the Spot
From my experience one of the most common things that happens is that you will need to make up a new character on the spot. The players are meant to walk into a bar and talk to a person. Of course, they decide to talk to someone else first on their way in. In these kinds of cases, you have a few options. You could just make things up on the spot and in a lot of cases, this will work out just fine. However, I find that I typically hit a wall when this happens enough times in a session. The most common issues here tend to be things like difficulty finding a name, a different enough personality or deciding on mannerisms. You could also plan out everyone in the bar ahead of time. That'll work, but I find that it tends to be more trouble than its worth. My preferred method is to have a cheat sheet of a few names, personalities, and mannerisms ready to go. That way, I can just grab a bunch quickly if I need to and continue. There is more power in this than you would expect at first since you can easily modify elements, or combines them together. There are long lists of names that you can use but for practicality I find it easier to have a smaller list ready to go as well. This list will need to be remade every now and then but this tends to take a couple minutes at most and I take care of this while planning the session.
There is another, more restrictive approach that works fairly well sparingly. There will be a couple of run-ins that you might want to have with certain characters. Typically, they are warnings not to go somewhere. However, they can be hard to fit in. Having the character and parts of their dialogue ready to go and throw in at a good time can be very effective. Since the players went to the barkeeper to ask a question, maybe he'll be the one to warn them about being out past dark. This is additionally made more powerful since you don't have to use the whole character. You can take elements (parts of dialogue is most common in my case) and give it to the barkeeper character you already wrote. You can bring out your other character later if needed, or if they avoided the barkeeper entirely. Everyone in town knows things are bad at night after all.
Think Like The Character
If you are new, you'll be understandably worried about doing things off the cuff. However, it's important to try to keep your character in mind and just go with it. Unfortunately, this is one of those things that comes with practice. However, it is also one of those things where Dungeon Masters have it harder than players do. Having to think up a character on the spot and play them is not uncommon for a Dungeon Master, and it's rather challenging at first. This isn't an excuse for lack of planning, but the attempt at improvisation is worth more than none at all, and it will be needed. Again, in this case it's about some restrictions imposed on us by the character we are playing. Part of the improvisation process is coming up with these restrictions. What do they know? What do they say? How do they speak? You'll be feeling for those questions and be making decisions right there. Like with names, you can have some cheats prepared a head of time. Still, I would recommend being some what comfortable with this situation because player actions can force you into this situation anyway. Just try to put your self in the character's situation and roll with it.
I tend to use dungeon tiles quite often. They can also be useful to help narrow down the general shape of an encounter location on the fly. You might have a couple of ready made locations (be it tile configuration or existing map) ready to go. You can also lay out a quick outline while describing some of the important elements. This is a bit of a skill that needs to be practiced as well but I found there are a few commonly used stall techniques. They aren't really stall techniques, since the Dungeon Master needs to specify these things anyway, but they do make it more seamless. After all, they don't need to sit there and wait. Thinking of any sounds, smells or obvious visual elements as they enter the room and giving them a quick mention eats up a few seconds. It tends to be one or maybe two, depending on how important the encounter is. An important element to think about is light (otherwise the human character will be confused about what they can see), hiding places, and sounds. You shouldn't make room interactions too long either. The magic is to be able to set up the encounter quickly while describing the location so your players don't ever need to wait. It's not easy to do and of course you'll miss now and then, but it's the ideal I try to aim for. Hit close and you are still fine. Dungeon Masters are often self-conscious of this situation, but players are often none-the-wiser and are just enjoying the game.
Another thing to keep in mind is that the rules are kind of malleable. Generally speaking, players prefer to play the game than rule lawyer in my experience. This means you can come up with your own when needed. You don't remember what the health of a skeleton is? Think of a number and use it. I say that with a caveat since it may set some expectations. Why was the zombie dragon we fought at level one so much easier? They are best handled when you have some justification as well (zombie dragon was clearly worn, couldn't even move as quickly, was clearly damaged, and made clicking sounds when trying to move). This will depend a bit on your group but I find it's more advantageous to keep things moving. At the end, think of standards, review the rules and make some notes for next time, or whatever else you need to do. It can be easy to forget a precise rule when you are new, when you are playing a complicated system with many supplements, or when you took a break. It will also happen eventually. Just keep going, try not to do it again, and when it happens don't let it grind the game to a halt. You have a rough idea, so you'll be reasonably close.
Write Stuff Down
If you had plans and things went according to them, you can get away with just reading over your planning notes in many cases. When improvising, this is not the case. Write stuff down. Maybe not at the exact moment since it's not always possible, but at least that day. It will save yourself a lot of issues down the line. Characters get filled in through interaction as well so you may need to make notes about your player characters that can be used later.
How reasonable things are is important to have in the back of your mind. This goes for both things you create off the cuff and for requests your players make. You want to let your players make creative actions. However, unless it's a very special kind of game you probably won't allow your ranger, without any arcana skill, to write a chalk circle allowing the party to teleport away. This goes both for how reasonable a player action is and how reasonable your response is. Again, there is no perfect answer for this as it depends on the group, but you should have a rough feel for it.
When improvising, it's easy to forget something. Even when you are not, there could be questions you forgot to consider. It's a big room and we have torches. How far can the humans in the party see? Expect questions. This isn't a bad thing, since it is a collaborative story telling. It also shouldn't make you too nervous. However, when such a question is asked it is best to address it clearly and quickly. If you are asked if they can see a creature on the tile, you could answer “yes”. You could also quickly point out which they can see and which they can't to avoid further questions. “How long will it take to get to that other city over there?” This is one of those that will need to be written down. It's also worth considering if the player character would know or if they would need to ask a potentially unreliable towns-person (well, he never has traveled that far in his life). Since players often ask questions, it should be no surprise that some level of improvisation is often needed like in the previously mentioned situations. However, I find new Dungeon Masters find these sorts of things easier to improvise than characters.