Don Barzini has it right. Isolated groups of humans develop distinct languages. As humans spread over the globe, they split into groups and tribes and new languages were born. Some of the most linguistically diverse places in the world (the Amazon, New Guinea, and the Caucasus) are diverse because of their geography. Tropical areas tend to have more languages than other parts of the world because in the tropics, there's an abundance of resources so people are more likely to stay put and their languages resist contact and are preserved. Mountainous areas have the same effect: the difficult terrain traps tribes in so they remain isolated and preserve their distinct languages.
When a language spreads over a wide area it changes over time, often enough that what started out as dialects of a single language has now become separate languages. It also tends to displace other languages in the process. As the Romans spread Latin over Europe, it evolved into French, Spanish, Italian, Portuguese, but also replaced languages like Gaulish, Celtiberian, Etruscan, and Oscan, all of which are extinct today, in a sense trading one set of languages for another.
Fewer people are isolated now, greater language contact is possible, and languages that have prestige are spreading widely so that in the end, there are fewer living languages today than there were in the past.