From generations 1-8, Pokemon can only escape if they use a Teleport. The player can use moves like Roar, Dragon Tail, Circle Throw or Whirlwind for force the battle to end. The battle ends because these moves are designated to swap the opposing Pokemon out.
Roamers naturally attempt to flee after every turn. You can block them from fleeing using a trapping move.
Gen 2 exclusively, some species of wild Pokémon can flee from battle. This can be prevented the same way as preventing roomers. The Pokemon able to flee are as follows: Magnemite, Grimer, Cubone, Tangela, Mr. Mine, Eevee, Porygon, Articuno, Zapdos, Moltres, Dratini, Dragonair, Togetic, Quagsire, Umbreon, Unown, Snubble, Heracross, Teddiursa, Delibird, and Phanpy.
In Let's Go Pikachu/Eevee, Pokemon regularly try to escape shortly after performing their cry animation. Unfortunately, this can't be avoided.
In Sun and Moon, Ultra Sun and Ultra Moon, there is a scripted battle against Nihilego during the player's first visit to Aether Paradise. This Nihilego, which cannot be caught, is scripted to flee on the fifth turn of battle. This flee can be avoided by using a trapping move. Source
Pokémon from the Safari Zone can flee, though it’s not really a battle.
Other than those particular instances, Pokemon cannot flee from battle.