Most of them learned languages at school. Or more precisely in a lyceum or gymnasium as these schools were called in French and German/Russian, respectively.
They gave a strong training in languages (and also in history, geography and mathematics). The languages normally taught included Latin (everywhere, until 20 century), Greek (ancient, of course), sometimes Hebrew, and "Modern Languages" (English, German, French). This was considered a normal education until the early 20th century, and was called "classic education". In many countries gymnasium was a necessary prerequisite to be admitted to a university. In some countries (Germany) Latin is a common subject even now.
One of my relatives, for example, born in 1910 in an educated lower middle class family in Ukraine would normally go to a gymnasium. His father was a worker (a typesetter) and his mother a housewife. Because the normal way of life was interrupted in Ukraine in 1917, the parents would hire private teachers (mostly students). As a result, he was fluent in English, German, French, Latin, and Hebrew, let alone his "native languages" normally spoken in Ukraine at that time (Russian, Ukrainian and Yiddish).
He became a scientist, in medicine. As I understand this was a standard for an well-educated person, especially a scientist (except Hebrew and Yiddish). After that time, the standards in language education sharply declined. Most Soviet scientists whom I knew and who were born in the later epochs knew at most one foreign language.
If you read Tolstoy, War and Peace, (the action happens in the beginning of 19th century) you notice that the personages (Russian nobles) speak between themselves in French and German most of the time, sometimes in English. They all had "classic education".
All education of Julien Sorel (Stendal, Le Rouge et le Noir) consisted of memorizing a Gospel in Latin. This alone allowed him to feel himself a gentleman, to be treated as one, and to qualify for a suitable job (early 19th century).
Before the middle of 19th century, the primary education was mostly Latin (the native languages were not taught at schools!) An educated person in Europe was first of all distinguished by the knowledge of Latin. Greek and other
foreign languages were taught later.
ADDED. As promised in my comment I made a little poll in a German university math department. I asked 9 professors and one secretary about themselves and their children. All but one had Latin and English at school, but Latin was not mandatory in most schools. One had just to choose any 2 foreign languages. In some schools Latin was mandatory and this depended on the Land (state in Germany) and of particular school. So I was wrong and corrected my statement.