Many articles &/or online advice suggests you need to do the following in order to become a master Dungeon Master:
Remember, this is role-playing. No matter what the DM prepares, the players must demonstrate choice and decision-making throughout the game
Why A Railroad? As mentioned: in role-playing players must have 'choice'. At any time a player should be able to do anything to anyone or anything. In simple terms: if you spend ten hours making an amazing dungeon in the west and players go any other direction (or simply stay 'in-town'), all of your work is for naught. They will never see it.
Is this a compromise? How does it work?
Most DMs plan out most of the 'skeleton' of a story, fill out the necessary adventuring bits for next session, then give players the illusion of choice. In the above example: if your dungeon is clearly to the west, players can go any other direction and they still end up in the dungeon. Perhaps there is a magic doorway-portal in town that sends them to your dungeon. To the east there is a waterfall that hides an entrance... and the tunnel goes west, to your dungeon. And so on. Giving players the illusion of choice but forcing specific content is called railroading.
Why a Sandbox? This approach means you allow characters to do whatever they want. The main problem with this is that they don't often figure out what to do and just may end up
1. Who Or What Is The Problem?
You need to know where this unifying issue came from and where it is going.
2. What Do 'They' Want? Why?
Someone figured out what The Problem was and has discovered-developed The Plan!
3. What Was Stopping Them... Until Now?
The Problem cannot be resolved by The Plan. Why not?
4. Why Will The Players Help?
A hero (or anti-hero) is anyone who realizes a problem and decides to change-fix it. In any story, show or book, the world finds its own heroes. This time it finds your player characters.
1. Who / What Is The Problem?:
Most D&D games have a unifying force - some horrible Problem that needs to be solved. It could be something as simple as an earthquake ('people buried & need saving... so little time!'). Usually a Big / Bad Evil-Entity (BBEE) is involved and they take otherwise interesting (yet otherwise neutral) circumstances and MAKE The Problem happen. This pivotal BBEE need not be big, nor even that bad... nor even a person. In a magical world it could be anything. Try these examples: an adorable daughter that accidentally manipulates her powerful parents ('the king and queen') whilst she mind is adrift in strange worlds. It could be a magic sword, tired of waiting for the hero to rise, is pulling strings to free an ancient evil. It may even be a fun-loving group of newly-minted cultists duped by a cleverly disguised demon lord.
Note that the trigger of The Problem need not even be sentient. Take the earthquake example from above: when disasters happen heroes can rise (and gain rewards). But if you have a prince from a nearby kingdom that wants the throne - and he has both a magical Box Of Ye Olde Grand Quakes to go with his nefarious plan - this makes it easy for you and usually more interesting for your players.
Anyone or anything - or even any group-faction - will do for your BBEE.
2. What your BBEG wants &/or why?: Both hero and anti-hero have a stake on the outcome. It doesn't matter if they want to be the prettiest at the Grand Ball or if they want to slaughter the entire world with a disease that turns everyone into undead. They want something - and, for a very specific reason which only you the DM knows, they cannot get it… yet.
In the above earthquake-disaster you could require that this Box Of Ye Olde Grand Quakes will advance his special Plan. Does he want the Princess to validate his claim to the throne? Ironic-specific revenge for the death of the rest of his royal family? Possibly the destruction is only a distraction - he knows of an ancient dungeon that will break open once the capital city is in ruins.
In any event, your BBEE focus-fellow has both a means and a plan.
3. What has stopped them until now?: When either the good guys or the bad guys have what they want, the game is OVER. For players this can be as simple or trite as: 'i got 2000 gold - now i can go make my adamantine half-plate armour!' Fortunately, you can write your villains to be more complex or at least not give up so easily.
That Not-So-Optional Fourth Step
Up to this point, making a 'meta-story' has not ruled out having either a 'railroad' or a 'sandbox' campaign. A DM can still force the players to resolve the earthquake-strikes or the players may just drink their beer in an over-engineered pub. What changes this? How do you make players care?
Ultimately, you cannot. If they would rather play Risk® or even send tweets on their phone, you cannot stop them.
That aside, you can offer their characters immersion: to be a pivotal and valuable part of the story's progression. How is this done without removing agency - that choice required for the game to be genuine role-playing.
Why are players usually invested? Why do they care?
If players are not invested, you do not have a game. Typically players are 'invested' in treasure, better gear ('magic items') and powering up ('gaining levels from experience points').
In your Earthquake-In-A-Box story you could have some reason why the characters could use this powerful device. Perhaps it is a key, a weapon, a component to power a great golem, a gift for a god or anything you like. Whatever your plot-device, your players will have a motivation that runs counter to your BBEE key-person.