Winston betrayed Julia because he was faced with his worst fear. We learn early on that he has a fear of rats. Near the end of the novel, he is brought to room 101. A wire cage is fitted over his head. Right next to him, with just a trap door between them, are two starving rats. If O'Brien springs the door, the rats will attack Winston, chewing off his face for food. Winston is so overwhelmed with terror that he realizes
that in the whole world there was just ONE person to whom he could transfer his punishment.
He screams—and means it:
Do it to Julia! Do it to Julia! Not me! Julia! I don’t care what you do to her.
This betrayal saves him from what to him is the worst possible fate.
This is done as a final step in breaking his spirit. All along, he had been utterly determined that no matter how much he was tortured, he would never betray Julia, his beloved. When he does this, his love for her dies. When sitting in the Chestnut Cafe he thinks:
"What happens to you here is FOR EVER," O’Brien had said. That was a true word. There were things, your own acts, from which you could never recover. Something was killed in your breast: burnt out, cauterized out.
He realizes that all his thoughts that he would protect her and sacrifice himself for her were illusions. He had also made the mistake of thinking too much about her, which has allowed the Party to know too much about his thoughts and feelings for her. As the Party can't tolerate divided loyalities—all loyalty must be to the state and Big Brother—they had to crush his love for Julia.