A shot from ballista, catapult or use of some sort of greek fire can also fuck formation up, but even after cannons and gunpowder became common formations were still used I believe.
Wizards are not that common in fantasy settings and can't cast spells too far or too often. There was that chosen of mystra from Forgotten Realms series who rode a flying chariot and did all the spells, just how effective you think she is compared to, say, a mortar squad? Can she lob hundreds of shells far as horizon non stop turning landscape into a moon surface? I doubt that.
Also, spell effects aren't consistent. How does exactly, say, a fireball behave? Can it be blocked? Or does it pass through and explodes inside a squad of enemies? If it's a piece of elemental fire, why does it behave like that? Does it have a knockback effect? These things change from one game to another, and you have to think them through before digging trenches.
Then take into account armies, thousands of soldiers, squads and flanks and all the logistics, and a handful of mages does not look that scary anymore.
Now I'm all for "there's magic in your world, deal with it already", but having a bunch of heroes running around casting fireballs does not equal to WW1 in my book.
I like how they do it in Warhammer, sort of. Wizards are few and on field of battle spell behaviour which can affect it can be erratic (which is represented by random spells and spell dice). Generally both armies field wizards to cancel each other out, with exceptions of "heavy casters" like elves. Then throw in artifacts and banners created by most powerful wizards in the world which cancel enemy spells and which you can stick to a squad, and you're kinda protected (depending on edition and metagame, of course).
But then again wizards there are special characters and commanders, and don't run around in squads of 20.
...er, last time I checked on Matt Ward they did not... yet.