In improv, it doesn't matter what the question is, the answer is always "yes" - and add more detail. For example, in a scene you might be asked, "Do you sell fenceposts?" A tempting response might be, "No - we don't sell anything". That might get a laugh, but it offers no new directions for the scene to go in and gives your partner nothing to work with. So just say "Yes" - and keep going by adding more concrete detail. "Yessiree, I've got a cow-fencepost here that even a 4,000-pound cow was unable to tip over." Now something is happening. That could trigger just about anything in your scene partner's imagination. "Oh, good! I've only got two 2000-pound cows, but they like to team up. Now let's see, I've got them enclosed in half an acre... How many fenceposts am I going to need?" Now, in just two lines, we've gone from nothing to the beginnings of a plot. - The Improv Wiki
Why talk about improvisation on an RPG blog?
Let's put it this way. No matter how amazing a Dungeon Master you are, and no matter how well you've written your campaign, or how excellently you've planned ahead, or how extraordinary the published adventure you're using is, it is impossible to account for player... creativity.
If you think you can predict the actions of four to six creative folk huddled around a table with imaginary swords and lightning bolts with nothing but a tiny bar of text that says "alignment" to guide them, you are in for a treat.
Sometimes, running a game can be stressful. I've seen many a Dungeon Master start sweating bullets as they brainstorm furiously for a plan to keep their campaign intact as they try not to panic while their precious plothooks were burned to the ground along with the tavern they were set within.
So, the players have killed the king's messenger after thinking he was a spy, hired no less then three members of the assassin's guild to kill the king after following a red-herring sidequest where the very same assassin's guild planted rumors that the king was a doppleganger, and have burned down half of the city in a fight they never should have gotten themselves into with the town guard.
Do you get angry?
Do you call the campaign quits?
Do you start hurling dice at your players until they stop?
No. You say "yes".
Players will have crazy ideas, but it's not your place to stop them. "But, but I'm the Dungeon Master! I control worlds! Universes!" A classic mistake that many Dungeon Masters, Storytellers, and Narrators make is thinking that the campaign is a movie, and they are the directors - controlling everything. This is an understandable mistake, but one that all DM's need to be aware is wrong. The only people who control the characters are the players.
I mentioned the hazards of critiquing roleplaying in my recent New DM Mistakes article, and the great rpg blog Gnome Stew actually has a pair of articles that tackle this issue directly, both on DM's not being the director, and how Dungeon Masters should never say that players are doing something "wrong". A player can't play their character "wrong". It's their character - not yours.
So, the issue of why you shouldn't say "no" is covered, but now, how do you say yes? Before getting into how to say Yes correctly, lets cover how to say Yes incorrectly.
Saying Yes, but while kicking and screaming "NOOOOO!"
"Sure, you can tie up the tailor and steal the silk shipment he's asked you to deliver... little did you know he was actually a MIND FLAYER IN DISGUISE! ROLL YOUR WILL SAVES YOU REBELLIOUS BASTARDS!" *evil laughter*
Yes, I know you're upset. Your carefully crafted plothook is being kicked in the face. You overestimated the player's dedication to your railroad plotlines. Put down the gun and no one will get hurt.
An unfortunately common response that Dungeon Masters have to players going off the beaten pass is to make it so difficult and frustrating for them that they will damn sure never want to bother again. This is usually done by making the actual act of "rebellion" incredibly hard to do - by raising DC's up to extraordinary levels, or suddenly making a level 1 commoner into a level 10 wizard half-dragon - or by going overboard on the consequences, making them unnecessarily harsh and/or deadly.
Obviously, there are always consequences for the actions your players take and you should make sure that such consequences occur. You should be careful however, to ensure that the consequences which occur are not unreasonable, unfair, or unlikely.
For example, if a messenger from the King meets the PCs on the road and the PCs then kill the messenger and bury his body somewhere - the King probably won't know. No matter how much you wish it was true, the King probably doesn't keep individual scrying beacons active on all of his messengers at once, and since this is a world filled with monsters and bandits, truth be told his messengers probably don't return intact or alive all that often anyway.
If you allow the PCs to kill the messenger, only to torment them brutally for it afterwards, this isn't "saying yes" correctly. This is punishing your players for their creativity, and your players will recognize what you're doing. Yeah... don't do that.
Saying "Yes", or Going With The Flow
The best Dungeon Masters are pros at going with the flow. If you have plot planned, and the PCs do something you aren't expecting, this shouldn't close any doors - only open new ones.
The hardest part about this is that it makes you have to think on your feet. Speaking as a DM who is not the best improviser out there, this can be hard - especially for new DMs. You have to greatly alter your plots all at once, and quite possibly even completely throw out what you were thinking.
There's two ways to tackle an unexpected event. Either, find some way to twist it, or some new way to approach the PCs to get them eventually back on track to your initial plot, or to indulge their actions and take the story in a new way following what they've done. As long as you allow PCs to be independent and to drive the story forward in interesting ways - rather then you driving the story with them just along for the ride - they'll be happy.
The trick, and the hallmark of a great DM, is to make absolutely everything that happens in a session look like you planned for it.
If the PCs go off the beaten trail, keep your cool, pretend like you expected it, and seamlessly string together what was your scripted plot, and what is now coming off the top of your head.
If you get upset, become overbearing with unnecessary consequences, start tearing your hair out, or generally get all mopey - the players won't have as much fun because in the back of their mind they'll know they're screwing up your plot. Since the PCs are the main characters of this story though, the plot is them, regardless of what you've written, so go with the flow and they'll think it's the best campaign ever.
Take a Breather
Do not feel ashamed if you need a moment to think. Every so often, the players will take the campaign in such an unanticipated direction that you literally have nothing to go off of.
Call a smoke break, or a snack break, and move away from your players. Take a pencil and some paper to start making notes about possible outcomes of your PC's actions. Turn it into a plot. Some of the best advice here actually comes from your players! If your players killed the king's messenger because they were CERTAIN he was an assassin... Well, maybe he was! Maybe these assassins captured the real king's messenger. If the PCs track down the assassins and rescue the true messenger he can give the PCs the same plot hook the original one was going to give, but now you've satisfied the craziness of your group's imagination.
If you don't have much time left in your session, you can also just try to hold out until the end of the session. The easiest way to do this is with a decent sized battle (which may or may not have any purpose) to slow the game down and stall until the session ends. Battles could also be used mid-session if you're good at multi-tasking. As the battle is waged you can scribble notes furiously behind your screen.
Either way, take a breather, and get some notes down - even just the most basic of guidelines. Break out a name generator, and any spare encounters/maps/NPC sheets you have kicking around. Create the side quest as you go, and constantly shuffle some papers around behind your screen to make it sound like you're going for a pre-made character sheet.
Put a smile on your face and offhandedly mention, "I was hoping you guys might do that". The players will think they gained access to a secret sidequest that you put time and energy into and will LOVE IT!
Regardless, learn to say YES! It will take some practice and some trust in both yourself and your players, but it can take a campaign in fantastic directions you'd never imagined. Have fun with it!