I don't know how law carrer works on other countries, but around here some criminal lawyers don't defend people accused of some crimes (like drug dealing, killers, etc.) and focus only on fraud, tax fraud, etc. "white collar crimes", mostly. That's where the choice element enters.
Defending someone guilty -like someone that confessed a crime - usually doesn't involve "saying the guy is innocent/nice". It usually means that sometimes the state does such a poor job investigating/pointing fingers that you can't be sure if someone is guilty or not. And that uncertain shows up for me in most of cases. Defending criminals basically means defending the idea that stat can put someone in jail for years based on uncertain data.
Uncertain? You see some weird stuff sometimes, i.e. two cops claim someone they were running after shot his gun a few times, trying to kill them. Guy was convicted by the people. The only witnesses were the cops and not a single bullet was found. And it was an alley. That's when you start defending people.
If I learned one thing in life is that sometimes you don't know even if you're right or wrong. From a personal standpoint, I'm never 100% sure if someone is guilty or not of something I didn't witnessed. That's right, I said never. (I don't use a tinfoil hat, but I have one in the closet.) Usually, you know for sure a crime happened - the whole dot-connecting thing that comes afterwards is the problem.
How do you do it: Since I'm not 100% sure of anything, I pick the best case scenario based on what the client and witnesses say and start from there. Then books, research, reading, etc.. On Brazil, the state has the burden to prove that the "best case scenario" didn't happen, it has to crime happened. That kind of defense is a technical thing, very different than defending a friend or relative, or an ideal, or something you believe. They aren't judging people, they're judging facts (here is the key thing - you're not saying someone is nice or not, the crime is judged. Only God judges people - or no one, if you don't believe in god).
My job is making sure the voice of the accused is heard and avoiding injustices while the state connects the dots.
That said, the whole "being a lawyer" doesn't drive me crazy. If I had to judge criminals, i.e., then I would have serious issues.