Let’s imagine an ideal situation:
FIFA’s Law 5 from the Laws of the Game 2011-12 notes:
Decisions of the referee
The decisions of the referee regarding facts connected with play,
including whether or not a goal is scored and the result of the
match, are ﬁnal. The referee may only change a decision on realising
that it is incorrect or, at his discretion, on the advice of an
assistant referee or the fourth ofﬁcial, provided that he has not
restarted play or terminated the match.
If play has not restarted, the referee can change the ruling. However, if play has restarted, no change can be made.
EDIT: Changed answer to quote FIFA’s Laws of the Game regarding referee decisions.