It's up to the ref. Pretty well described above, so won't repeat, but I will add that an actual advantage has to have been gained, not just an opportunity. For example, your team has the advantage call, and you put a player into a gap with a great pass - he should score a try, but drops the pass. There was no advantage just because he had the opportunity, by dropping the ball there was no advantage.
It's up to the referee. If the advantage is for an infraction which would result in a scrum then it may only be a couple of phases or if they other team secures possession. If it's an advantage for a penalty then it can be even longer. If the other team commits an infraction or they aren't clearly benefiting (such as scoring or creating go-forward) from the advantage then they will go back. So in short, it's all up to the referee.