When do I have to use 'will' instead of 'going to'?

Solution 1:

Using will (or shall) is the proper way to form the actual future tense, and is completely generic. IT can be used in any case in which you wish to refer to the future.

Going to + verb is a shortcut construct that is commonly used in many situations. It is typically used to express occurrences in the near-future.

In many cases however, particularly in colloquial speech, the two constructs are virtually interchangeable.

Wikipedia has some additional notes on usage:

Going-to future is used when the speaker wishes to express certainty about the future based on evidence or fact from the present or the speaker's opinion: "If you do not stop, you are going to be caught by the police and hauled back to jail." "Our houses are going to be swept away by the impending storm." While the "will" and "going to" constructions are often interchangeable, only the "going to" construction can denote former future intention (e.g. "I was going to eat dinner, but decided not to").

Solution 2:

Some ESL courses seem to have a strict set of rules for when to use "going to" and when to use "will" which don't reflect the way native English speakers use them. Much of the time, you can use either one of these constructions.

The way I use them, I say "going to" when I'm talking about plans that are being made, or I've just realized something is going to happen.

If she can't come home this summer, we're going to fly to Venice in June to see her.

Watch out! That rock is going to fall on us.

But you can certainly sometimes use "will" when something has been planned for a long time, and is definitely going to happen, even if there is no uncertainty about it. Almost everybody uses will when talking about the sun rising:

The sun will rise tomorrow at 6:43 am.

On the other hand, if you want to pass your ESL course, you may have to learn the rules it uses.