An Infield Fly is a fair fly ball (not a line drive or bunt) that, in the judgment of the umpire, can be caught by an infielder, pitcher, or catcher with ordinary effort and when there are runners on first and second or first, second, and third and less than two outs. When the umpire calls "infield fly” the batter is out, regardless of whether the ball is subsequently caught or dropped. The ball is live, and runners already on base may advance (at their own risk) if the ball is not caught or tag up and advance if it is caught.
Simply put - it's in effect if there is a force at 3rd base with less than two outs.
Why does it exist?
To prevent double or triple plays on popups. Imagine bases loaded and a soft popup to third base. Without the infield fly rule, the runners have a dilemma. If they stay close to their bases, the third base player could choose to let the ball drop and get multiple outs by tagging the runner who was on third, stepping on third base to force out the runner from second, and throwing to second base to force out the runner from first. If the runners go too far from their bases, the third base player catches the ball and doubles them off. The reason the rule applies only with at least first and second base occupied is that if there are not multiple runners on base, a missed popup can only result in one force out.