I saw the following 2 options in an exam question:
私は__に行く
a. 外で遊び
b. 外へ遊び
The answer is '外へ遊びに行く'
Does 'で' also work here? What's the difference here?
外へ遊びに行く (head outside to play)
-> The implication here is that the actor is not currently outside, and will head out to play.
外で遊びに行く
-> This sounds odd because it means that the actor "goes to play" when he's already outside. It's not really clear where he would be going, if anywhere.
This is similar to saying something like, "go out outside" in English.
I think the fundamental thing here is that the main verb is 行く, which is a movement verb and thus has a directionality associated with it. It requires a destination.
For now, let's ignore the 遊びに, which is used to specify intent/purpose for the 行く action. Then the sentence basically boils downs to
外で行くvs 外へ行く
Here is where the directionality matters. 外 is a location, but で tells us that we are already at 外. Yet, we don't know what the destination is, making this is very confusing and unnatural. Meanwhile, へ is typically used to specify a direction/destination, making it the natural choice here (に is okay too). Note also that you could say 外で遊ぶ but not 外に遊ぶ or 外へ遊ぶ. This is because the verb 遊ぶ doesn't have an directionality so it's natural to specify the location and unnatural to use a direction/destination particle.
Okay, so let's talk about the 遊びに now. It's interesting because unlike with many other parts of Japanese, moving this feels quite odd. However, because both 外へ遊ぶ and 外に遊ぶ are just invalid, in this case it's rather straightforward I think.
Thus, if you said
遊びに外へ行く
I think everyone would understand you. It's also grammatical. Because of this I think it's fine to just consider the simplified case above to decide which one to use.
There are some strange grammatical quirks thought about the Verbに行く construction as mentioned in the comments on @sazarando's answer, so I'll talk a bit about what I found with regards to that. In On restructuring infinitives in Japanese:Adjunction, clausal architecture, and phases by Masahiko Takahashi, he talks about the grammaticality of swapping the sentence
僕が図書館に本を借りに行ける
into
僕が[本を借りに]図書館に行ける
I think the interesting thing about this is that, first, the clause [本を借りに] is considered optional and, secondly, that the clause moves as a unit. But, based on the paper, I think this behavior is limited to when there is a clause where the Vmasu in Vmasuに行く is being used (i.e. 友達に会い, 本を借り). So that situation doesn't apply here because 外に遊ぶ and 外へ遊ぶ are just not grammatically valid.