Here is the rule for the ranking of the teams in the Group Stage of the World Cup, from Article 41 of the Regulations, 2014 FIFA World Cup Brazil
COMPETITION
The league format shall be used: each team playing one match against each of the other teams in the same group, with three points for a win, one point for a draw and none for a defeat.
The ranking of each team in each group shall be determined as follows:
a) greatest number of points obtained in all group matches;
b) goal difference in all group matches;
c) greatest number of goals scored in all group matches.
If two or more teams are equal on the basis of the above three criteria, their rankings shall be determined as follows:
d) greatest number of points obtained in the group matches between the
teams concerned;
e) goal difference resulting from the group matches between the teams
concerned;
f) greater number of goals scored in all group matches between the teams
concerned;
g) drawing of lots by the FIFA Organizing Committee.
The teams that qualify from the group stage will play the round of sixteen.
If, after 90 minutes, a match ends in a draw, extra time of two periods of 15 minutes each will be played. If the score is level after extra time, penalty kicks will be taken to determine the winner in accordance with the procedure described in the Laws of the Game
The winners of the four matches in the quarter-finals will qualify for the semi-finals.
Source: FIFA