The sandbox works well because it has clear boundaries (hence sandbox). There's a sort of meta-game, social-contract thing where the DM says to the players, "You can do whatever you want, as long as you stay within this area. Want to skip the dungeon and just start slaughtering townsfolk? Be my guest. But if you leave the valley, I ain't got no D&D material prepared, so you'll either have to suffer my spotty improvisational skills, or else we end this session and pick up next week."
This is important. "Sandbox" does not mean -- cannot mean -- "no limits." It means freedom and agency within whatever limits are set. That's why you can have both a single dungeon and a whole campaign world operate as sandboxes -- it depends on the parameters set up by the DM and agreed to by the players. Note also that there are other limits on sandbox activity that rarely get discussed, the most important of which is probably intra-party consensus. While it is certainly possible for a DM with infinite time and creative resources to run each individual player separately through their own adventures, practically speaking the group is going to operate largely as a unit. Therefore, the collective agency of the players is a limitation on each individual player to some some degree.