I see several factors at work in the D&D worlds, especially in 3.X: Rarity, Motivation, lack of solidarity, Social Contract, and the frankenstein effect.
Rarity:
Wizards and Clerics are surprisingly rare... typically under 1% each of the population using the 3.0 DMG methods. The smaller the segment of the population, the harder it is to take over.
Motivation:
Most wizards and clerics have no motivation to take over the country. It's a matter of time and duties. Remember, rulers are typically doing a full time job; half paperwork, half public appearances. A typical medieval ruler had 20-30 hours a week spent on ruling, 10-20 on courtly duties, and a large chunk of the year making the rounds to keep everyone reminded who is in charge.
Clerics have detailed obligations to their deity. Non-adventuring clerics probably work the same 20-40 hours per week as did historical clerics. And unlike historical clergy, they have very clear and powerful indicators of their deity's displeasure... like the loss of spells.
Wizards have a tendency to devolve to studiousness and experimentation. This study and experimentation takes time and effort... 40-60 hours a week when making big magic items. Plus, it's the type of activity that prevents going on progress (the travels a lord made to keep tabs on his see), and that can't be interrupted safely nor cheaply.
Fighting men, however, have no such limitation on their time. Practice with weapons can be done while travelling, and besting your vassals in personal combat with rebates is a good way to remind them who is the boss.
Social Contract:
All governments boil down to two factors in the social contract: protection of the people and payment for same. The Government is paid taxes to ensure the safety of the common man. A small oligarchy is seldom capable of ensuring everyone's safety. A larger one, like nobility, is far more capable of so doing, because it is a significant fraction of the overal population. (Likewise, most non-noble-based governments won't be having universal suffrage, nor even majority suffrage, either, so are essentially oligarchies anyway.)
Lack of Solidarity
Clerics are almost always going to be divided amongst several deities in the typical D&D settings. This takes a small group, usually about 1-1.5% of the population, and divides them into several smaller groups.
Wizards are a contentious, proud, and studious lot. Like most academics, ego is a dangerous part of the equation. Academics seldom overthrow extant governments, and usually can't retain power once they get it. Wizards have big threat power, but unless they divert from their studies, they won't keep up, and can be taken down by another wizard. And like most other forms of academic, there is the desire to not share one's research without recompense... Which tends to mean a drive for secrecy in research, and not a whole lot of cooperation. The wizards guilds mostly exist to protect the wizards from each other first, and the rest of society second, and so seldom make moves to take over, since that would make them target #1.
Further, Wizards and Clerics scare people. Real power, demonstrable, is a potent cause of fear... so unless they have the confidence of the locals, taking over means overcoming the populace. And if they have the support of the populace, odds are, they don't have reasons to take over.
The Frankenstein Effect:
"Enough screaming mad peasants with pitchforks can overcome any force"
The common man is nothing to be sneezed at, when in sufficient number. While A person is usually fairly smart, People en mass are dumb, panicky, prone to overreaction and violence. Any group of wizards trying to take over by force without popular support wind up decreasing the population and either fleeing or dying.
Remember: A person can't take 20 on a to hit, but a mob of 20+ is bound to interrupt your somatics, and likely trample you to death. Or throw rocks at you.
Sure, a missile reflection spell can stop the hurled stones and veggies... but it only lasts so long. And as soon as one hits, more will follow.
So why do they occasionally exist?
Because, sometimes, the local leadership is bad enough that the populace implore clerics or wizards to take over. Sometimes, civil policy is going against the wizards or clerics, and is sufficiently unliked that the guild or church can take over without a general panic.
People as a whole tend to support the status quo if they have enough to eat, safe food and drink, and are not being routinely beaten. When civil governments get to the point where people no longer feel safe, whomever promises to make them feel safe can get into power. And once in power, as long as they don't scare the peasantry too bad, will tend to stay in power.