This is one of the most difficult things to deal with, particularly as an inexperienced GM.
A lot of peoples' view is that railroading, where you limit player choice to the extent that there is no choice, should never happen at the table. I tend to think this is a little extreme.
One of the things that varies a lot from group to group and game to game, is how 'sandboxy' a campaign is. A full sandbox game dumps the characters in the world without any signposts at all and lets them decide what to do. At the other end of the spectrum, are heavily plotted campaigns, which can include significant amounts of railroading.
Most games fall somewhere between the two, and the thing is, different players and GMs enjoy different amounts of railroading. This means you need to understand what your players want from the game and what their expectations are of you. Do they want a sandbox where they can do what they want or not? This is a conversation you should definitely have with your players at the start of the next session so everyone understands and agrees how the game is going to work. This is particularly important if your players are as new to roleplaying as you are.
Onto your specific situation. If you're faced with your players going in direction you have nothing planned, the first thing you can do is tell them that you are going to need a short break. This is a perfectly OK thing to do, and gives you time to go and look up monsters for an appropriate encounter or two. If you think its going to be too difficult to come up with something on the fly then be honest and tell your players. Explain to them the problem, and ask them if they mind going in the direction you have stuff planned for. Almost all players in this situation will go along with you as long as it doesn't become a regular thing.
If you're nearing the end of a session, there's also nothing wrong with stopping a little early so you can prepare for the next session. At the end of each session I also always ask my players what their general plans are for the next week. This really helps to avoid too many major suprises, and can give you a general idea of what to prepare. Of course, it isn't a fool-proof system, but it helps.
There are also a few things to help prevent that kind of situation coming up too often.
You can create floating encounters. These are broad, generic encounters that are not tied to a specific location. You can drop them into the game if you're in need of filler or don't have anything specific planned for what the players are doing. It doesn't take too long to build up a collection of these to add to your arsenal as GM. Also, re-purpose and reuse things you have prepared that for whatever reason never came up.
Make sure the big plot you have planned ties into the character motivations that your players have. If you haven't already done so, ask your players why their characters are adventuring. What is it that they are aiming to achieve? Doing this naturally gives more motivation for players to go in the general direction you want.
Make sure the 'main' plot is appropriately signposted. If the players don't know it exists or realise how important is, they won't follow it. This is a difficult one to balance, as if you go too heavy handed then it can feel very rail roady and forced.
When you're designing your campaign, plan situations that go on with or without the players' intervention. Know who the major players are, how they relate to each other, their broad resources and what their aims are. Providing a situation that changes with time will help you work out encounters that might happen based on what the players do. Knowing how the major factions are set up will help you work out the forces involved in any last second encounter.
The last thing to emphasise is that improvisation of any kind when you are a new GM is really, really difficult but it does improve with practice. Take it slow to start with, be honest with your players, and they will most likely be understanding as you learn your trade.