In simple terms, no hyphen is required in your second example because no ambiguity arises as to the intended meaning without one. We understand that the school teaches a foreign language . Phrases like this comprising a head noun ("language") and an attributive modifier ("foreign") are called 'composite nominals'.
New words, one handy idiom, and a 2-minute quiz — delivered to your inbox to keep your streak alive.