Move Priority

Priority Moves
+4 Pursuit (on a switch)
+3 Switching
+2 Detect, Endure, Protect
+1 ExtremeSpeed, Mach Punch, Quick Attack
0 All moves not listed
-1 Counter, Mirror Coat, Roar, Vital Throw, Whirlwind

A faster Pokemon always goes first within the speed bracket of the move. In every single case, higher priority moves go before lower priority moves.

The 'exceptions' are Metronome, Mirror Move, and Sleep Talk. These have a priority of 0, and thus if you Sleep Talk Whirlwind, for example, you can go before a slower Pokemon using, say, Tackle.

Roar and Whirlwind have to go last to work, however. If the Pokemon using Roar / Whirlwind moves first, the moves fail. This can happen by selecting those moves with another move or if the opponent is using Counter, Mirror Coat, Roar, Vital Throw, or Whirlwind and is slower.

Pursuit comes right before a switch if a switch is used. It doubles in power if they switch out. If they use Baton Pass and you are faster, the Pokemon using Baton Pass takes normal damage. If they use Baton Pass and you are slower, the Pokemon they pass to takes normal damage.

Quick Claw gives your Pokemon a 23.4375% (60 / 256) chance of ignoring Speed and going first within your speed bracket, barring the enemy also activating a Quick Claw, in which case normal Speed rules would apply. Speed and Quick Claw never let a move with a lower priority beat a higher priority (so a Quick Claw activated Aerodactyl using Roar always goes after a paralyzed Shuckle using Tackle).