How on earth was the cordyceps created?

I first came to know about the fungus a few months back, while watching BBC’s Planet Earth series.

This amazing/creepy/cool fungus attaches itself to a host arthropod, and then releases enzymes which affect the host’s brain, effectively gaining control of the brain. The host ends up doing what the fungus wants it to. For example, the cordyceps attacks an ant, and then under its influence the ant climbs up a tree branch and dies; after this, the fungus grows from the head of the ant (like a scene directly taken from a sci-fi movie, says David Attenborough).

Amazingly, there is a unique cordyceps species attacking each of a thousand or so species of insects and caterpillars. Moreover (this from Wikipedia), there is a particular species of this fungus which is supposed to have a very high medicinal value, and accounts for a large chunk of Tibet’s economy.