My recent employment adventure turned me cynical. My then-soon-to-be boss seemed great during the interview. He was technical, his questions were interesting and deep and the team seemed happy. Turns out he was a complete sociopath who made everyone's life living hell. The unhappy weren't invited to the interview.
The first clue from the second day at drinks should've been, "I voted no in hiring you, but [his boss/my previous company's CTO] convinced me to change my mind." in front of the other senior admin. Kinda downhill from there.
What makes these situations so difficult is that leaving a job early has such a strong negative effect on your future work prospects. Even worse, being unhappy at a job affects your work prospects, too. There really isn't a win in those situations.
It's a pretty good time to be a developer - most of us don't need to hang around under asshole bosses just to keep the rent paid next week. If you start a new role, and the red flags are waving madly on day 2, strongly consider walking out.
This advice won't apply to first-job-put-of-college devs, in that case you might just need to suck it up for a year, and put off buying that flash new car or taking that trip to Vegas until you've got 6 months living expenses saved (Note, may also not apply so well to fairly well paid junior or mid level devs who've gone way too deep with SF housing rental either... Same advice applies, build up a six month living expenses savings, so you _can_ afford to walk out on an asshole boss if you need too...)
But yes, other people did know what type of boss he was. The last recruiter I worked with said, "You worked for X? That guy was fucking mental..."
The aggravation can make you want to lose your cool, but like you said, not backing down is key. Nothing is worse than someone telling you that you are not doing your job and then never challenging them with an intelligent response. You then have to spread your gospel like you own it. Otherwise, you become guilty due to your perceived indifference.
I think it's kind of like dating: if they don't give you an obvious signal, you probably won't know until it's too late, and sometimes when you get an "obvious" signal you might be wrong anyway.
The only advice I can think of--beyond heeding obvious warning signs--is to try to work some place that's doing something you actually care about. Then you're less likely to run into incompatibility issues, and the effect of toxic people may be mitigated.
Another angle is that you probably can't avoid working with assholes, so you can get organized and be prepared to push your own agenda. Don't back down too easily. I've seen this work fairly well, but you have to be ready to deal with the stress and potential fallout. Depends on circumstances, YMMV, etc.