Friend, never do that
Gornemant of Gohort, Perceval, The Story of the Grail
The problem with players going off the rails is the rails.
The advantage traditional role-playing has over video games is the shared storytelling. Lean into that advantage whenever you can.
If the players "go the wrong way," or refuse to take hints, you might be tempted force the characters into the action you want, or whinge at the players that they're making it hard on you as a DM. One is railroading the characters, the other railroading the players. Neither is fun for anybody.
You provide the plot. The players, the twists.
Players really can't "go the wrong way" and move away from the plot, because they can't control what happens next, you do. Instead of forcing the characters to the action, move the action to where the characters want to be.
Those Reluctant Heroes
Let's say your NPC King bestows a quest on the party to strike out into the Forsaken Wastes to find the Lost Caverns of Desolation, your awesome dungeon for the next phase of you campaign. But your party decides they love life in the big city. What's worse, they want to join the thieves guild.
So instead of a few wilderness encounters on the way through the Forsaken Wastes, they commit a few challenging property crimes, and become respected members of the guild. Then the old guild master tells them about a grave threat the guild faces, that has set up a stronghold in the sewers or crypts below the city. (Maybe, if the party can neutralize the threat, the old guild master will retire and give them mastery of the guild.)
D&D plots, like any fiction, are written to be rewritten
It just so happens the layout of the underground complex they discover looks a lot like the map for the Caverns of Desolation you had drawn up. Just replace the hobgoblin hillbillies with wererat hipsters, and you are all set. You might even find the new setting is more compelling - and count on it that your players will.
Vested Players are easy to guide
If you've given the party a chance to make a place of their own choosing in a world, they will be much more likely to accept a quest to defend the things they've chosen and worked for.
Plot details are constraints only for as long as you choose
Sometimes it's a little tricker, taking @Anne Aunyme's example from the comments:
If there is a villain who will destroy the world in exactly one year if nobody stops him and the only clue that can bring the PC to him is in one specific dungeon, you can't let them completely skip it.
All these details are defined by the campaign and can be "rewritten" by the GM.
"The only clue" is a motif in mystery campaigns - it sounds cool, but it's a symptom of railroading. Add another clue that lets the players proceed through the plot in a way better fitting with their character stories.
"One specific dungeon" - again, when there's only one thing the party can do, that's when they are most likely to rebel and reject. But, if you must get them to a particular place, let them pursue their interests for a bit, get them really mad at some villian, and then let said villian disappear through an Irresistably Tempting Blue Portal™ to your dungeon. See if they don't follow him. (But typically, you can just move the dungeon to the setting of the player's choosing.)
"Exactly one year" - unless other heroes, or the gods themselves, intervene to buy the world a little more time. Or the characters dawdle, and the world is destroyed, but the party is transported back in time to try again. Or the villian was wrong, the world isn't destroyed, just drastically changed - the party now needs to proceed through the plot to bring it back.
Giving the players a little latitude will almost always get them back on board, pretty quickly.