The short answer is that both meanings are applicable and the only way to know is from context.
The problem here is that in spoken language, なに tends to be shortened into なん, such that the two meanings "by what means" and "for what reason" overlap. If one wants to be clear, one should say なにで when referring to "by what means", and in writing it is good to not use the kanji in this case. In written text, they should be distinguished (なに for "by what means") but since the text in your image refers to spoken language, it is shortened there.
I remember having the same question when I first started learning Japanese many years ago, also with that book. However, I cannot really recall having heard it used in the sense of "by what means" in real life, people will instead say なに. Perhaps there is a regional difference, I've mostly lived in Fukuoka.
Here is some data from a survey I found: http://nhg.pro.tok2.com/reserch/reserch1-3.htm
The survey asks "Do you use なに or なん when asking about commuting method?". In total, 71.4 % prefers なに. In 近畿{きんき} more people prefer なに than in 関東{かんとう} , although only 105 people were interviewed so no strong conclusions can be drawn from this survey.
「何でいきますか」 as "In what means did you go?" ... "Why did you go?"
<-- "did you" should be "will you", right? It's いきます, not いきました. They're talking about あした !