It's based off of this formula:
-A is the current Speed of the player's active Pokémon,
-B is the opposing Pokémon's Speed divided by 4, (mod 256)
-C is the number of times the player has tried to escape during the battle (counting the current attempt)
a number is randomly generated, if F is greater, then you can escape, if not, it will fail.
Several other conditions can cause escape to always succeed or always fail:
* An opponent Pokémon with the ability Shadow Tag causes the player's escape attempts to fail if the player's Pokémon does not also have Shadow Tag.
* An opponent Pokémon with the ability Arena Trap causes the player's escape attempts to fail if the player's Pokémon is not Flying-type or levitating.
* The reusable held item Smoke Ball causes its holder's escape attempts from wild Pokémon to succeed.
* The Pokémon dolls can be used to escape from wild Pokémon.
* Trapping moves used by the opponent can prevent the player from even making escape attempts.
* If the player's Pokémon is under the effect of Ingrain, it will be unable to escape.
* The ability Run Away causes escape attempts to succeed and ignores anything above that would prevent escape.
* In Generation II, it is impossible to escape when battling a shiny Pokémon.