Fungus have evolved some very complicated biochemistry, since they are actually highly evolved lifeforms. See for instance, the amazing antics of slime molds.
Since colonial insects, like ants, concentrate resources centrally in a tight area, it makes sense for a fungus to adapt to exploit that. There are a number of paths that could be taken-- There's the symbiosis route, (
https://en.wikipedia.org/wiki/Ant%E2%80%93fungus_mutualism) and there is the parasitism route.
Conjecturally-- It makes sense for a fungus to start out by first being an opportunistic decomposer, where spores get deposited onto an ant and then sprout when the ant dies. This has a potential evolutionary path to become an active infector, if there is an advantage to sprouting "early", while the ant is still alive. (If the ant still feeds, and moves about, the fungus gets more nutrition than it otherwise would form a dead ant's corpse, AND it gets mobility to spread spores opportunistically through shared cleaning behaviors of the ants.) From there, manipulating ant behavior to improve those outcomes seems reasonable...
Throw into this that insects and fungus are some of the oldest complex lifeforms on our planet, and you have plenty of time for mutual adaptations, both beneficial (such as with mutualism), and antagonistic (Fungus starts puppet-mastering ants to maximize its resource collection and spore distribution-- Ants adapt to discard infected ants VERY far from the colony.)