The way that magic biomes work now is that there's an integer value for Evil and Savagery throughout the map. When this value is high enough (such as > 75 evil, I believe), it will make an evil biome in the area. (However, the actual areas affected by evil tend to be stretched out to fill whole oceans or mountain ranges rather than having patches of evil in an ocean, currently... that might have to change to make the spread of magic biomes more gradual for in-play use.) Good is just a very low Evil rating, by the way. (< 25 Evil.)
So, if you want to make a very crude system for how this would work, you could have some sort of magic energy counter, where every unicorn puts a certain amount of good magic into the area (which would be like -10 points of evil per unicorn or something, and then -1 point for a bubble grass), and every 100 or so points of energy that is put into or taken from the area will permanently affect that regional Evil biome setting by a point.
So, if you let a cow eat some bubble grass, it adds a point of evil. If that grass regrows, it subtracts a point of evil. Each unicorn that dies is +10 evil, each unicorn given birth to is -10 evil.
If you go and burn away or graze away 40 tiles of bubble grass and kill 6 unicorns, you'll add 100 points of evil magic, and tick over the change on the whole region, adding a point to the Evil Biome score. If that is enough to push the total Evil Biome score above 25, then the area stops being a Good biome, and the unicorns will retreat for "gooder" lands, and the bubble grass might not grow back any more without encouragement.
If you somehow encourage more bubble grass growth or buy some trapped good creatures from elves that you breed and add to the good energy (negative evil magic as this game puts it), then you can make the biome into a Good magic biome once more.
That's a basic mechanic for the system, but you can make it more complex, especially when you start having multiple types of magic...
For one, you can have an "overall magicality" in a biome, and then have a "flavor" of magic, so that, even with powerful evil or good magic "flavor", if you suck all the magic out of a region, you won't see any magic effects at all. (It would be similar to a neutral region now.) Meanwhile, if you have powerful magic but drain away how evil that magic is, it might still be magical, but just be neutral magic instead of evil or good magic.
So what you'd have is a "how powerful is the magic" meter, and then a "what kind of magic is it" meter. (This "flavor" meter would naturally be multi-dimensioned, though...)
This talk about "flavor" of magic also lends itself well into the breakup of magic biomes - when we have sphere-based magic biomes that Toady wants to move into, instead of having just good, evil, and savage, we might have sky magic and sea magic and land magic biomes, and in a sky biome, everything might fly. (
This thread on brainstorming some sphere magic might be good to look at.) So all sorts of magic creatures and things might be in a region, and producing magic of their own particular flavor, and if there's significantly more sky magic than any other type of magic, then it starts turning into a sky biome, and the natural creatures in the area might start getting turned into sky creatures that might be like normal snakes and tigers and badgers and such, but now they fly because they're sky-badgers.
Another major portion of the way that Xenosynthesis is supposed to work, however, is that not all things actually add to magic, some drain it.
A skeletal normal creature, for example, might actually drain some evil magic from an evil region. Killing it would release some of that evil magic. If you work from this draining idea, then there's a finite limit to the amount of skeletons that a single evil region can raise before it neutralizes itself for raising too many undead, and you can think of it as a natural sink on the regional supply of evil magic. In the case of a sky magic biome, meanwhile, then the flying badgers might also be magic-sinks - when the sky magic in the area gets too high, they release some of that excess sky magic by zapping some normal creature into being a sky magic creature.
You also have things that you might purposefully raise - like magic plants - that sink magic because they need that magic to survive, and if there isn't enough magic in the area, they don't live. Growing too many of these might temporarily neutralize the magic, and you'd need to somehow push more magic into the system to keep feeding those plants. (This could also work for purposefully raising specific animals that suck up magic.)
When harvested, however, they no longer consume magic, and you might end up with high quantities of excess magic when you finish harvesting some magic plants.
Since this excess magic manifests itself by zapping some creature with some odd powers, if the magic levels deviate by a significant enough degree, instead of just giving a badger some wings, it might create something more chaotic and dangerous, or else do something like summon a sky-magic-aligned megabeast, like a roc, or create a titan or forgotten beast on the spot to sink some of the excess magic into. (The same way that corruption, when built up in the thaumcraft, sinks excess magic into bad stuff that comes back to haunt the player.)
When there's a high concentration of magic all of a sudden, magic things happen, and it's just the "flavor" meter's job to tell what exact type of magic will surge wildly.
Now then, for the concept of spirits, the best way to think about it would be to use the sort of "multiverse" concept, as Toady already has talked about different dimensions, like shadow dimensions where the bogeymen come from, or the imaginary dimension from
Cado's Magical Journey.
Spirits and gods usually just hang out in their own dimensions, but they can come into the DF world when there's a big enough space that's covered in their flavor of magic to support them. (They sink magic of their kind.)
Alternately, if there's a large enough zone of magic created, a spirit may be born of the magic, itself. (Or gods can be created through people worshiping that magic or that spirit if we want to go down that path of gods that exist only through the accumulated faith of their believers.)
Because they need magic to survive, they will want to see magic in general boosted, and especially magic of their flavor. (Although they will probably despise magic of an opposing flavor, like a sky spirit hating land magic or something.)
Spirits and gods may also get nourishment from whatever it is their sphere most associates with it, as well, however. So a justice-aligned spirit/god might gain nourishment from a hammerer executing a murderer, and may become slightly ill from each crime unpunished.
Sacrifices to the gods (such as a sacrificial ox, classically,) would also be a nourishment to the gods.
Feeding the gods or spirits gives them power, and in return, they'll likely share some of that power with those who actively do things that feed them, like spreading their type of magic, or giving them sacrifices in order to encourage more sacrifices or spreading of that magic sphere's influence.
A spirit may well be "trapped" inside their magic field - almighty powerful inside their own realm, but incapable of surviving outside their magic biome at all. Hence, they would want and need henchmen to spread their magic biome out beyond their borders. (This would be the forest spirit using elves as its emissaries and protectors.)
When there's spirits or gods in a magic zone, they would have the (game-simulated) sentient capacity to determine how excess magic in their area gets spent - hence, if they are angry with your dwarves, they'll make sure every scrap of excess magic they have is spent on creating more forgotten beasts to spew poison on your dwarves. If pleased, they'll just create some excess winged badgers or something.
Of course, if you
do piss off a spirit enough, you can also just "kill" it by draining all the magic power it needs to live away from the region by killing the local magic-producing plants and animals.
So, basically, the "balance" on magic is that if you let magic gain power, it's going to have a semi-sentient spirit that's going to demand things of you that you have to do to keep it happy, or the magic will become hostile to you, and rain organ-melting blood on your parade.
Finally, as for the whole thing going kaput, I think it might actually be okay for dwarves to be capable of causing the extinction of all magic. (And if they need magic for themselves to survive...) It wouldn't be a bad thing to create this space where dwarves have to keep the magic in order to survive so that you can't go too far to one direction or the other - too much magic causes Ragnarok, while too little magic means dwarves and goblins and elves go extinct, and humans and mundane animals are all that's left. It would certainly be the dwarfy thing to do to ultimately cause the extinction of their own race.