To me, one essential element of the roleplaying experience is the symbiotic relationship between the player and the GM. I don't believe players can "ruin" a GM's plot, because the plot is there for the enjoyment of the players, as much as the GM. If the players don't follow the plot of a GM, they didn't ruin the game, but rather evolved it to something the GM didn't expect.
One the other hand, tabletop rpg should also be fun for the GM. As I've said many times before, if the GM and players don't work well together, this may ruin the roleplaying experience for all involved, but how do you deal with this when you are already in a session you can't get out of it?
As someone fairly new to GMing, I've learned that the best trick, which has a learning curve, for a new GM is the ability to improv. Use what you have and make it interesting. For myself, in a long campaign, I use NPC and villain motivation mostly to change plot, while in one-shots I tend to just let the players shape the world.
A few tips for new GMs if your players "go off the rails":
1. Don't panic and step back for a second. Actually, going off the clearest path in a session is a sign of an imaginative party. Think about creative ways to handle this new situation.
2. Either shape your world in depth or prepare for it to change at a moments notice. When your players change what is happening in ways you didn't expect, either fall back on elements of the world that you already exist or just let your PCs run with what they're doing. They think the mundane water is magical? What if it was?
3. Know the fun quirks of your system or have someone in your group who does. Some systems have weird mechanics or fun aspects that can liven or change a session, such as someone losing sanity or the whole party falling into a dream. What do you have to work with? Use it!
4. Use your knowledge of your players and their characters. Does one player like to start fights in game? Maybe bring up the possibility of a bar fight? Is a character afraid of spiders? Throw a spider down that hallway.
4. Don't be afraid to ask for help. Just don't have an idea? Maybe your players do! They changed plot already! Maybe they want to change it more! It's okay to not entirely control situations as a GM!