The cold truth of planning is that you will plan things that don't get used. That's just how it goes. How much of your planning goes to waste depends on how far out you try to plan.
The trick I use is to divide my planning up into higher level ideas, and low level details (like stats).
High Level - Far Out
If your campaign has an overarching storyline, big bad guy, or major plot point, you need those figured out significantly in advance. You should know in general terms what your plot entails, what your big bad guy is doing, and so on. This is done at a high level, you do not need detailed stats for the kitchen staff of a building your players won't see until the end of the campaign.
This is also useful for plots you might want to run in the future, or places the PCs may visit. Write down some very brief thoughts on what you want to do there (I suggest Evernote as a tool for this, it's free and handy). When they get closer to it, you can come back to those notes and add details, but if they surprise you and go early, you still have something to work with. Best of all, this takes very little time and you can do it as you come up with ideas rather than requiring a dedicated planning session.
That's one of the reasons why Evernote is good - you can do your main planning from a PC or something, then add a quick note from your phone later if you think of an idea.
Low Level Details - Nearby and Relatively Soon
When you are looking at details, you don't want to plan too far ahead. The problem is that unless you're running a plot where you know with certainty what the players are going to do (and can control it if they try to deviate), they will surprise you.
In my current campaign for example, I know pretty well what's going to happen next session. I know that because they're highly likely to continue what they're currently doing, as it was something that's still in progress. I can plan for that, and I can plan to the end of that "module" or subplot relatively reliably. I'll have details for that for a session or two in advance, in case they go faster than I expect.
If there's other things to do in the same area, I'll also have details for those. Who knows, they might stumble onto an NPC and decide to help him instead. I'll want to at least know what I'm doing for that.
I don't have details planned for a plot that's two towns over from where they are right now, and that they likely won't get to for three sessions. I will have a general idea of what I want to do there (and have some very brief notes in Evernote to track that general idea), but won't have done details yet.
The reason why is that it's far enough away that the PCs may just decide to go in another direction entirely, and any detailed planning is wasted. If they wind up going somewhere before I thought they would, I use the higher level notes I have and improvise for that session. You can do a lot if you have those high level notes, even if you don't have details.
If it's something that I really can't improvise, there was one time I Just had to explain "sorry guys, I'm not prepared for this yet. You can do it next session." Most of my players have also been GMs, and they were understanding.
This doesn't totally prevent the need to make up some things on the fly, but on the whole it lets you have a very high success rate in terms of your planning time going to things that actually get used.
Another thing that can help is to have a random encounter or two ready to go. Then if you want to add an encounter, you can bring that out without having to prepare something specific. (Assuming your players like that. Mine aren't big on pointless combat encounters, so my "random encounters" tend to more often be some NPC doing something that's totally unrelated to anything the party is doing. Kind of a slice of life of the town moment.)
In my current game, the players in one session set some events in motion that led to a kidnapping at an embassy in town. At the time, I wrote down some thoughts on that and filed it away.
A couple of weeks later, I came up with ideas on how to use that. So I expanded the notes, including coming up with NPC names and general roles in the embassy. I had an NPC pitch it to the PCs. They were not enthusiastic. No big loss, as this didn't cost me a lot of time.
Later on the PCs changed their minds and wanted to go through with the plan. Their plan involved breaking into an embassy and kidnapping someone the embassy was holding prisoner, in order to ransom that person back to the embassy for lots of gold (which the embassy would pay to avoid public embarrassment and news about the person being there at all getting out).
It was only now that I did the detailed work like statting out the embassy guards and coming up with a floor plan for the place to use as a map. Had they not taken the plot hook, I knew that before I had invested time in that kind of work, so the amount of effort wasted would have been very low.