For worldbuilding I recommend starting with broad strokes, then fill things in with tiny details first and grow out from there as needed. That may sound weird. What I mean is start with the big picture. Is your world a spherical planet or a flat Earth type situation, possibly something like Discworld where it's being carted around space (or the Astral Sea) by some giant elephants riding an even bigger turtle? Once you have that, scale down a bit one step. Are there multiple biomes/climates like deserts, jungles, temperate forests and savannah plains, or all a giant water world with scattered islands as the only landmass populated by mermaids, pirates, and one guy that uses a Simic Hybrid stat block and looks suspiciously like Kevin Costner?
Now think of the type of the story you want to tell and what the world needs for that. This may actually supersede the previous steps if it requires specific fundamental elements of the entire world but we're still working a mostly blank canvas inside of that frame you have so far. Tying in with "what about when players do something unexpected?" I recommend not planning the story too much ahead of time. I like to think of it like how professional wrestlers (who are really just theater nerds who also like getting jacked and doing all their own stunts) prepare for a big showcase match. They usually plan a few "spots" which are the key turning points of the narrative in their match. These spots are also usually what shows up most on highlight reels, big flashy moves like somebody getting thrown out of the ring, a particular high flying move, a third party interfering, and of course the big finish. They mostly improv all the action between the spots (sometimes they only have the end planned and improv the entire match but unless you're looking to run a pure sandbox game and just pull everything out of your ass as you go you should at least have something planned for your next session). Plan your spots as key events to advance the story. Make sure to drop plenty of hints and story hooks so your players know where to go for the next spot to happen. Plan for what you want them to do, anything you think they might do differently, and still try to leave wiggle room to improvise because they just might do something completely different. Start filling in the details around those spots, starting with the very beginning of the game. What's your hook to interest the PCs in the main story? Where is it? A tavern, a castle, a guild hall, or some empty field in the middle of nowhere that they all happen to be in for some reason? Then build out from there until you have a framework for that spot with necessary NPCs and locations like shops, taverns, dark forests, spooky caves, and haunted castles. Then other details like towns, cities, nations, and such that these places can be found in. But remember that you're going for a finely detailed picture frame to let your players paint within by their decisions because this is a game of collective storytelling and you're like the conductor of a symphony who is leading the team but not actually playing an instrument. Okay mixed metaphors but hopefully you get it. Then once you've figured out what they're doing you make adjustments to your next spot as necessary and drop hints, hooks, leads, etc to lead the party to that. Fill in the canvas between your frame as you go along with the help of your musicians. Sometimes you might have to change or move the next spot to fit with the way they reacted, like deciding an oval shaped frame is better than the square you started with for what's now a painting of a tuba instead of a piano. Also you're now out of brass colored paint, maybe use some of the leftover wood tones, ivory, and ebony for the next frame (this means if they skipped an NPC or plot twist you intended to be important just move it elsewhere and work it into the next segment of the story).
As to players going "wildly off course" that could be a few different things. First, are you all looking to be playing a game around the concept of linear storytelling (what I just described is intended to be a flexible approach to that) that follows a single cohesive and continuous narrative? Or is that player running off chasing butterflies or whatever wanting to play a sandbox game where everybody just does whatever strikes their fancy at the moment and expect you as DM to make stuff up on demand? Sometimes a player will say they want the latter but really just take pleasure in actively avoiding anything the DM provides and those people are sadistic assholes. I don't like to play with those. If this kind of thing happens often and you don't think they're just being dicks but rather just missing or misinterpreting the hints and leads you've presented, I recommend just talking to them about it. Admit that they are way off what you planned for and so far outside the lines you can't even hear what instrument they're playing. Or something like that I'm tired and fixated on mixed metaphors now. Very specifically ask them what they thought about you telling them the things that you intended to be hints and clues pointing at the stuff you have prepared and figure out why they missed them. It could be you not making things as obvious as you thought, or maybe your red herring was too smelly, or they're just dense and completely missed obvious clues. Unless they're just being a troublemaking jerk who brought a vuvuzela to the orchestra it's probably actually some combination. Talk with them and figure out how to more effectively communicate so everybody has fun and stays at least close enough to what you have planned and the music of your story doesn't stop with the sound of a record scratch because someone is expecting a DJ to drop beats for the freestyle rap of a sandbox game.
Okay I think I might have overdone the metaphors there but I'm tired and found it amusing. Hopefully some of that made enough sense to be helpful. Maybe take it as an example on improvising weird shit that just popped into somebody's head so that it fits with the story you had planned. Because I'm now insisting that the mixed metaphors tacked onto the advice I intended to give from the start are themselves metaphors for player generated chaos. That's my story and I'm sticking to it.