I realize you need to 'counter' a stack - but how do you do that when the units that line up to defend against your attacker seem to pop out at random?
They don't pop up at random, the best defender against a given attacker is always chosen. Let's assume he has a crossbow and a pike. So if you attack with a knight, he defends with the pike. If you send a maceman, he defends with the crossbow instead.
The same happens ofc if he attacks. For each of his attackers, you defend with your best defender against this particular attacker.
So far, the defender seems to have the advantage. Enter artillery. For each attack with an artillery, your whole stack takes "collateral damage" (up to six units i think), no matter if that artillery dies or retreats. After enough artillery fire, your stack is so weak that it doesn't matter if you defend with your best unit. You just get slaughtered. And the AI loves to build artillery, it is often from a third up to more than half of their entire stack.
On my difficulty level (a humble noble/prince), you should expect those stacks around the late medieval ages, early renaissance. Sometimes earlier, but rarely (i think it depends on the size of the rivals and the warmongering they have done).
None the less, my way is the way of the turtle. I love designing impregnable fortresses. I can't give much advice for offensive wars , but I made good experiences with the following counter measures, most of them already mentioned:
- Don't defend in cities. Due to artillery, the city defense bonus goes to zero in the fraction of a round, leaving him usually with enough to launch the assault in the same turn. Plus, he will have city raiders, so you are sitting on a tile with virtually NEGATIVE defense properties. You would be better of on a simple grassland tile in that case.
- Attack his stack with artillery of your own. It doesn't matter if they all die, it WILL weaken him due to collateral damage, even if the defending unit will take no damage at all. At the very least, it will delay his attack (while he waits for heal). Think of them as one-way-units.
- Build enough artillery

I often neglect that, and I curse myself every time that happens ...
- Try to meet him on a tile with natural defenses, such as hills or forest, behind a river, or all of that together. Especially forest gives more defense than any city could ever grant you, since his artillery can't bring it down. You might consider building roads to such places and fortresses above them (fortress pro: can't be taken down by artillery. con: is subject to city raider promotions).
Even if he takes that fortified position, he will waste much of his artillery in the process. Without artillery, he can't take cities.
- I tend to prefer "Drill" promotions above "Garrison", even for my garrison units. Drill gives you first strikes, AND makes the unit suffer less collateral damage (maybe psychological, but it seems a really significant difference). And in my experience, mid to late game wars are all about surviving collateral damage.
(try a protective leader or the ethiopians for that)
- Siege engines suffer no collateral damage, and machine guns count as siege engines. They are a turtles best friend, until tanks show up.
- I sacrificed a city once like kazapp described. My heart bled, but it worked, and I felt incredibly clever

. It is not something you would want to do on a regular basis (well maybe if you found cities for that sole purpose. name them accordingly to avoid confusion! hehe). But if the alternative is getting wiped out completely, it sure is an option.
- I had no success with the flanking cavalry so far, but it seems i just had never enough of them. I will try that the next game i get horses

. Cavalry is another thing i tend to neglect it seems ...
That all being said, I find the city raider promotion a bit silly. It should help to ignore defense bonuses, but it shouldn't come to the point where defending a fortified position is detrimental compared to defending an unfortified one. But hey.