He wins out of a massive sympathy vote when they kill off his runningmate who was the late John Spencer (character Leo McGarry).
They had been planning to let Alan Alda's CA Republican win but Spencer died giving them a reason to give it to Smits instead.