Whenever my players roll before they establish their actions in the fiction (my system is Dungeon World), I say something like: "Whoa whoa whoa wait a moment. What are you doing and how are you doing it? We do not even know yet whether a roll is even required for that."
I then have them explain what they do and if it triggers a move (=rolling), I'll have them roll again. Any rolls before that are invalid.
Even though they know that premature rolls are invalid, they still do it occasionally. However, for me this is a simple and clear rule to handle these situations: a roll is only valid when the GM has prompted it from the player.
Because it is an easy and clear rule. It also follows from the rules. In Dungeon World, for example, a move always follows from the fiction, and a roll always follows from a move. Thus, a roll can never precede the fiction. I presume it it similar in most game systems.
What alternatives are there?
If you as a GM want to grant your players a bit more autonomous freedom, you can of course define situations in which players can roll on their own. However, these must be clearly defined situations. For example, in my games I do not prompt for a damage roll after a successful Hack&Slash roll, because a player always rolls his damage in this situation.
A roll is only valid if it's been announced before the dice were rolled. Usually, it'll be announced by the GM asking for a specific skill, but a player could announce a roll if they think it fits the action. Irrelevant rolls are ignored (and replaced by a relevant announced roll) not because the GM didn't ask for them, but because they don't fit the proposed action. This could be a good compromise between making sure the players don't cheat or use the wrong dice, and not interrupting gameplay with the GM calling every roll (credits to 3Doubloons).
Other than that, if your players cannot show the discipline for basic rolling and rerolling rules (die off the table or stuck on its edge), you probably have deeper interpersonal conflicts that you might need to resolve.
I've played in and run evil campaigns of various sorts in both 3.5 and 4e (though not 5e, I think my learning will transfer), and run into a lot of problems: My Guy Syndrome comes up a lot, as does a tendency to default to a regular D&D storyline only with more stealing of spoons and kicking of puppies to remind ourselves we're evil. Sometimes an evil campaign instead descends into over-the-top motiveless violence until there's no story at all. There's a whole host of at-the-table and in-the-story issues, and I tried many different strategies to address them. Eventually I came up with a framing device which works well for us in avoiding these problems:
Provide the PCs with a Master to guide them toward orchestrated works of Evil.
Start the game with the PCs as underlings/minions/hirelings/apprentices/etc of a powerful evil NPC. The Master has a complicated Evil Plan and he tasks his minions to enact various parts as the Plan progresses: "Bring me the soul of a hound archon," "Raze the border keep," "Steal the Apocalypse Gem," "Help a spy infiltrate the paladin's ranks," and so forth, tailored to the PCs' abilities.
This provides the party a reason to work together despite having different agendas (and working together will hopefully bond them as friends so that they want to continue as a group) and establishes small achievable evil goals that accumulate into an Epic Evil Event.
All you need to do is ask the players to make sure their characters have a good reason to work for the Master: The serial killer likes having his rampages subsidised (and the Master protects him from the Law); the necromancer seeks to learn from the Master's experience and gain access to his libraries of forbidden lore; the mercenary's in it for the money and benefits.
Eventually the Apprentices will surpass their Master.
Expect the party to betray their Master at some point, hijacking his Evil Plot for their own gain: this is not only expected, but awesome. It's the Master's Evil Plot, not yours, and the story isn't about the Master--it's about his apprentices. Consider the Master to be training wheels for evil, setting an example which the party can then follow to surpass and overthrow their instructor as they level up.
This works because Evil Needs Goals.
As Ed describes so well and AgentPaper elaborates in the D&D context, evil needs concrete reasons motivating its actions. The Master provides goals and motives while the players find their feet in the new paradigm, channeling and guiding their exploration of what it means to be evil in ways compatible with the D&D paradigm without simply kicking puppies during a dungeoncrawl.
A word of warning: Alignment is tricky.
D&D has a history of the details and nature of alignment sparking major heartfelt arguments, because D&D alignments are not easily (or appropriately) matched to real-world philosophies and moralities; they're narrative simplifications to support the game's conceits and draw their power from storytelling conventions rather than from genuine moral complexity. Exactly what this means and how to deal with it are beyond the scope of this answer (and possibly this site, although there's a LOT of questions on the topic you can look at), but you should be aware it exists and be ready to talk with your players about what "Evil campaign" means to them so there aren't nasty surprises mid-game.
In the moment, ad-lib something.
This happens to me all the time. Despite all my preparations, my players often completely subvert or circumvent my planned sessions. Just last session, they were at the start of a whole dungeon I planned, but they went "This looks too scary," left, and went to scam some vampires in a city I had barely named.
When something like this happens at your table, in the game, all you can do is make something up. It's not easy, but if you just grab something reasonable-sounding off the top of your head and flesh it out, it might be sufficient to get you through the session. Sure, you're probably not going to blow anyone's minds, but you can still have an entertaining improvised session.
Stuff like that gets much easier with practice, however. You'll eventually get the hang of coming up with what you need to keep the game running, and even have some fun/interesting encounters.
Prepare for the unexpected by fleshing out settings
The long-term solution to this is to have a flexible setting, instead of a single railroaded quest. In addition to planning out specific quests and events, you should also decide specifics of your setting: what are the major industries in the area? How active are the gods? Is there a centralized bandit gang, and do they have weird rituals? If they set that inn on fire, what sort of guards will come after them?
You probably won't use 99% of what you come up with, but thinking about such things ahead of time allows you to ad-lib much more effectively, because you already have foundations and prompts to work off of. It also doesn't hurt your actual session planning, either.
Know your players and your characters
It's true that in a TTRPG, anything is possible. However, the realm of what is likely is a much, much smaller subset of those possibilities. It is important to have a session 0 in order to determine the kind of game and characters your players want to play, and you can prepare around that.
For example, if your party is full of lawful do-gooder paladins, you probably don't have to worry about them committing random acts of evil. Likewise, if you know that your players spend lots of time every session messing around with NPCs, you can spend more time focused on making interesting NPCs instead of planning a ton of combat encounters.
In this way, you can give your players freedom to do what they want, but also be prepared for what they'll likely do.
If all else fails, railroading isn't always bad
Railroading is frequently bad, but it's not always bad. If you've planned for a world-ending cataclysm to happen, then let it happen regardless of what the PCs are doing. For instance, after opening a portal to the Abyss, my players decided to simply leave and do something else. As a result, I had the portal steadily chew up huge swaths of the game world until the PCs decided to do something about it.
For your bandit example, your players can mess around for a day or two, but the town is going to get ambushed regardless of what the PCs do. If the PCs decide to go to a completely different town, you can just reskin your bandit ambush to target that town instead, and the PCs will be none the wiser. After all, the players only see what they play--they won't know that random town A is actually town B in your notes.