This is determined by a Pokemon's Speed. The Pokemon with a higher speed will attack first, with a few exceptions.
Certain moves, like Quick Attack, give the attacking Pokemon a higher priority when attacking. Other moves, like Avalanche, give the attacking Pokemon a lower priority. The Pokemon with the higher priority will always attack first, regardless of speed. If two Pokemon have equal priority, however, the game will use speed to determine who attacks first.
There are also moves which will modify the speed of a Pokemon, like Agility, which increases the speed of the user. There are also moves, such as Bubble which will decrease an opposing Pokemon's speed.
Moves like agility are called Stat Modifiers, and increase/decrease a Pokemon's in-battle stat by a number of stages. A stage just represents the stat modifier the Pokemon currently has, and by default the modifier is 1.0. In total there are 13 stages, 6 which increases the stat, 6 which decreases and the default stage which has no effect. It should be noted these stages have a strict upper and lower limit. If you try to boost a Pokemon's stat stage beyond the 13 stages the move will have no effect.
The stage modifier values are:
1/4, 2/7, 1/3, 2/5, 1/2, 2/3, 1, 3/2, 2, 5/2, 3, 7/2, 4
Some moves will have unique effects which will also modify who attacks first, such as Trick Room, which modifies the rules so the Pokemon with the lower speed will attack first.
Certain abilities can also have an effect on this. Speed Boost is a good example of this, which increases a Pokemon's speed at the end of every turn.
There are also items and berries which can modify a Pokemon's speed. Two examples would be X-Speed, which increases a Pokemon's speed; and Salac Berry, which increases the speed of a Pokemon when its health drops below 25%.