There is no way to prepare for only and exactly what the players will explore in a sandbox. Two major ways of solving this have emerged over the decades:
Prepare Everything
The first and most obvious method is to solve the problem with brute force: prepare everything in advance. This is extremely time-consuming, and is a project that few GMs ever actually complete despite their good intentions. Those that do manage it, do so because they focus on a single world for many real-world years and consider world building to be a worthwhile hobby by itself.
Build the world Just In Time (JIT)
JIT is a relatively recent computer-science term, but it describes the most common successful sandbox-building strategy: build only what you need when you need it.
In practice, this means you start with a small slice if the world and write only very brief notes (as short as one sentence or even one word) for each feature, and then expand your notes only when needed. This expansion is either done between sessions just like in the "prepare everything" method (except limited to just this one element), or is done using improvisation and (semi-)random generation tools and techniques; or more often, a mix of both.
For example, a map might have a mountain on it and the single word "Dragons" beside it. You don't need to know more until your players get within sight, rumour, or near travel distance of the mountain. You certainly don't need to know the name of each dragon and 1000 years of the mountain's history when the players are still apprentice adventurers 100 miles away! You may not even need to know what that mountain and word mean when they're 20th level if they never went in that direction and now they're exploring the political situation on the Elemental Plane of Earth.
When the players start looking like they're going to be nearby that mountain though, that's when you prepare it and flesh it out. "We want to explore Dragon's Mountain next session!" means you know what you should be preparing this week! Even that much warning isn't needed though, with tools and improv. Any idea you could come up with during between-session prep is an idea you could come up with during the game. If you have stat blocks and creativity, you can make up an encounter on the fly. If you have random tables, stat blocks, and creativity, you can improvise an entire complex of mountain valleys, factions, leaders, monsters, names, history, and treasure during the game.
This method of Just In Time game preparation is very efficient, and leaves you a lot of time to search out tables and tools to help you flesh out your notes during play instead of spending time detailing every candle maker and blacksmith in a town they might never visit.
Of note is that the most significant preparation is always before the first game of the campaign, because you need to detail the starting area (often a town) more than with just a single word on the map. But with people-generation tools, a town can be improvised to life from very thin notes just like the larger world can be. Prepare the start location and a handful (two or three) locations of exploration/adventure, and let your players show you where to add detail to the world.