On the counter-measures front, a typical small town that is serving as the urban hub for a 8-12 villages and hamlets has the following resources to defend itself.
a) A garrison of the guard: Usually 10-20 mounted soldiers, together a number of pages, messengers, and grooms. They are generally 1st-2nd level fighters, and are in service to whomever is the local liege - often the sovereign but depending on how feudal a particular nation is possible a local feudal lord. They'll be lead by a 'Captain' who is a capable fighter of 3rd-5th level. The closer the town is to the border, the more capable such garrisons tend to be. There primary job is to deal with small external threats like individual highwaymen, and relate up the chain of command any potentially larger threat. Somewhere in a 1-2 days march is usually a small castle with a larger garrison of 100 or so troops, with a commander who can call together all the distributed forces if needed. By switching horses or riders at every smaller garrison, a messenger can generally send a message 60-70 miles in a day and further in emergencies (pony express style).
Each garrison has a small supply of emergency items for dealing with unusual problems - for example, lycanthropes. This will generally include silver arrows, magical arrows, specialized poisons, and thrown weapons of various sorts (grenades, if you like).
b) A town watch: Generally 8-12 individuals who operate as thief catchers and in modern parlance 'police'. They are generally 2nd-3rd level hunters (they get more work than the guard), with a smattering of rogue levels (takes one to catch one). They generally have a number of well trained dogs or similar pets. They are led by a Reeve who is generally both a tough guy and a fairly shrewd politician. In general, they tend to be a rather sneaky ruthless bunch of individuals, and in addition to the usual skills are fairly adept at using nets, lassos, mancatchers and the like. They are pretty adept at ambush, subduing things, cutting out tongues, gouging out eyes, and generally rendering things helpless. 'Due process' means ensuring a potential spellcaster can't do bad things to you before the judge arrives to give you permission to execute him, or feeding everyone the same poisoned feast and ensuring you have enough antidote or clerics on hand to cure the innocent. Most of them take the opinion that hard ruthless individuals are necessary for good men to sleep in their beds at night. Some of them are serious about that. Others just use it as an excuse to be mean, sadistic, and extract bribes or engaged in legalized banditry.
c) An ecclesiastical counsel: Generally 4-8 clerics of 3rd-5th level, commanding a collectively a force of 10-20 Templars with various capabilities but often 1st-2nd level fanatics, and assisted by a variety of initiates, novices, and non-combatant laity. Note that, as is fitting in a society where the gods play such a huge and active role, in an average town this might well be the most capable single battle force. More importantly, the local temple can generally send a message to a parent temple in the big city were in essence awaits an NPC party specialized in dealing with problems like undead, werewolves, witches and warlocks, or whatever uncanny problems might plague a town. Or in other words, they can call 'Paladins' down on you.
d) Local Hedge Wizards: Generally 2-4 wizards of 3rd-5th level who are residents of the town. They aren't necessarily directly useful in battle, but will generally make themselves useful in whatever capacity they can if the town is under threat. Most are fairly capable craftsman, experts at potion making, and know a fairly wide range of utility spells.
e) Improvised Line of Battle: This depends on the town, but generally each town has some additional resource of some sort that in an emergency can be called on. In a port town, this might be those hill giant laborers who work as stevedoers in exchange for full bellies. In a forest town, this might be calling down out of the hills the lumberjack crews with their mastadons. In an ancient town, this might be calling on the towns own 'small gods' - the various fey and spirits who normally live invisible lives along side the town. It might mean that the towns ancestors have sworn some potent oath witnessed by the gods to protect the town in the event of utmost disaster, and suddenly a force of supernatural beings appears from the lands of the dead. It might mean that the city has pact or convenient with an outsider, and there is a sphinx, or genii, or archon that shows up whenever the town is in mortal danger - perhaps animating that big statue in the town square where gifts are offered every year. In a town along a major caravan route, it might mean that the PC's probably aren't the only sellswords in need of work. In a town with a less savory past, this might mean opening that sealed crypt of your long dead great-great-great grandfather and dripping some blood into his skull, or sacrificing one of the town's children on that particular altar in that grove no one normally goes into.
f) The Militia: Worse come to worse, you can call in every franklin, yeoman, esquire, and knight within 10 miles and make a small army.
Bigger cities will have comparably bigger resources to draw on.
Generally speaking, I've never had a problem with 'murderhobos' and never had to resort to 20th level town guards and Eliminster. The assumption of my setting is that since there are generally functional and stable societies somewhere in the setting, they've long ago figured out how to deal creatively with murderhobos of every sort, including the PCs. The general answer is, "We don't stand and fight on their terms."
Now of course, this isn't the default approach. Lots of people can get killed and property destroyed if you have to go all in versus some high level characters. And in general, a village of 80 people really couldn't do anything immediately about high level characters pushing them around. But generally speaking, no one likes getting pushed around either, and towns prefer to go to war before discovering just how dystopian life can get under the reign of some amoral terror. Or conversely, if the powers that be are immoral or amoral terrors, they prefer to act before they are replaced. And remember, no matter how ruthless the PC's are, these are towns that have to live with and deal with threats that are potentially far more ruthless - that is they see the inhabitants only as a food. There are monsters out there. Most of the time towns have to deal with such threats without having a uniquely powerful band of heroes around.
If the PC's really get to be so powerful that societies can't protect themselves from the PC's, well, someone makes the rational decision to just let the PC's lead until a better arrangement can be found. These probably will only happen though after the PC's have made a few examples and sufficient demonstrations either of ruthlessness or real leadership ability. The PC's defeat of higher level NPCs, usually with some aid and assistance from the local town can be considered an example of what would happen if town went after the PC's.