Feeding them to my military.
Also I am disappointed no one mentioned magma.
If I'm using magma, I usually don't take them alive in the first place. Easier just to build a magma chamber and have it purify out the troll wool socks for the pure, sweet, goblinite.
Anyway, to answer the original question, I generally just plain DON'T execute goblins and elves I capture, as odd as that may be. I tend to think like an engineer, so I prefer to use them as
efficiently as possible.
I take out a few as punching bags, of course, letting some keep their armor to make them take more punches, even, but I tend to prefer using them as components of my great dwarven machines. Goblins make for fine animal logic, since they have no need to eat and are immortal. Just let them run around naked between great contraptions they have no hope of understanding for all eternity in a sealed-off chamber long past the time when the mechanism it powered has crumbled to dust, and anyone who knew of his existence has long since died. I like to think that some future civilization will some day find these few straggling goblins, still running this loop, thousands of years into the future, and wonder at what sort of forces created this pitiful green creature. It certainly will have gone so far past mad by then that it wouldn't be recognizable as having more intelligence than a slug...
It happens to still be cruel, yes, but it's
profitably cruel.
For the elves, I tend to strip them naked and put them in my zoos, with some of the other entertaining savage beasts. Well, mostly, I tend to just cram my dining hall cages with all the turkey poults. Still, leaving elves to spend the rest of their lives starving while being crammed cheek-to-waddle with 40 pecking, scratching turkey poults right before seeing them pulled out one-by-one for butchery as they hit adulthood for the rest of their lives seems a fitting non-execution, as well. Plus, some dwarves enjoy watching elves in cages for their "grace", which I presume is code for "willingness to keep talking about how much they love trees and animals while starving to death in a wooden cage with food animals biting them in the ass."
Any halfway-talented enemies tend to keep their weapons and get pooled for getting thrown down to an FB for my entertainment/to spare my military the threat of a syndrome. If they succeed, they have a chance of escape off the edge of the cavern map. (Unless I have built a recapture hall already...)
Necromancers, of course, are just plain too valuable as weapons for use as anything else. They are to be given a supply of entertaining animals in a graveyard, and pooled for flooding some entertaining threat to my fortress with undead.
Loud Whispers can show you how it's done.