Critical hits are determined by a variable and a random number (from 1 to 256). If the random number is less than the variable, then the attack will hit critically.
The variable starts at zero and is determined by a few things:
If Chansey or Farfetch'd have the Lucky Punch or the Stick equipped (respectively), +2.
If the attack is Aeroblast, Air Cutter, Attack Order, Blaze Kick, Crabhammer, Cross Chop, Cross Poison, Karate Chop, Leaf Blade, Night Slash, Poison Tail, Psycho Cut, Razor Leaf, Razor Wind, Shadow Blast, Shadow Claw, Slash, Spacial Rend or Stone Edge, +2.
If the Pokémon has used Focus Energy, +2.
If the Pokémon has the Scope Lens or Razor Claw equipped, +1.
If the Pokémon has the Super Luck trait, +1.
If a Dire Hit has been used on the Pokémon, +2.
If the variable is 0, the random number must be at most 16 (6.25% chance, or 1 in 16).
1 = 32 (12.5%, or 1 in 8)
2 = 128 (50%, or 1 in 2)
3 = 256 (100%)
Note that 3 is the highest it can go.
The moves Storm Throw and Frost Breath always land Critical Hits.
Note: when a move scores a Critical Hit, damage is calculated based on either the modified or unmodified attack and defense stats, whichever is more advantageous to the attacker. For example, an attacker who has used Swords Dance and then makes a critical hit will get the benefit of the increased Attack, while one affected by Charm will use its normal Attack score.
Thanks to Uiru's Pokémon DataBase and Bulbapedia for most of the information on this page. Also thanks to Knight of Time for providing more information.