The best way to understand is through examples. Take this conversation, for example. Two friends are in a restaurant, looking at a menu. B knows what he wants to order, but A hasn't decided yet.
1. A: Have you decided yet?
2. B: Yes.
4. A: So, what are you going to order?
5. B: I'm going to have steak and fries. What about you?
6. A: I'm not sure. Maybe I'll just have a salad.
7. B: You'll be hungry later if you only have salad.
8. I'll share my fries with you if you want.
9. A: Thanks, but I'm trying to lose weight.
10: B: Look, there's sushi on the menu. Why don't you have that?
11. A: Oh, yes.Good idea. I'll have the sushi.
lines 4 and 5 : They use 'going to' because they're talking about something that has already been decided. It's a plan/ intention
line 6: A is unsure, so uses 'maybe ..will'
line 7: This is a first conditional, so B uses 'will' . (Though 'going to' would also be correct here).
line 8: This is an offer, so B uses 'will'
line 10: This is a new decision, so A uses 'will'
I agree with everything that the other members have already said. Dina has given you some textbook rules, but as James says, these rules are not hard and fast, and in many cases you could use either form (or indeed other forms entirely). And as Frances says, 'going to' is used more in conversation, whereas 'will' is more of a feature of formal and written contexts.
I hope this helps.