How does justice take place on the cross?
That's a very deep question that I won't be able to answer fully. But Jesus takes the punishment we deserve. Similar to how you're in a court of law, if someone pays your fines for you, you're free to go. Jesus paid the fine on our behalf. The wages of sin (payment for sin) is death, but the gift of God is eternal life through Christ Jesus our Lord.
Jesus, in his very nature God, chose to humble himself down to the role of a servant. He even partially limited his omniscience and power, and became fully man, fully God.
It wasn't the physical torture that hurt him so much, but it was the wrath of God beating down upon him on that cross. He was separated from his Father, whom he had been with for all eternity. That spiritual separation was pure agony. He even sweat blood before going on the cross, because he knew just how agonizing it would be to be separate from his Father, and instead of receiving his love, he would receive his wrath. The wrath that we deserve.
He freely chose to be punished in our place. So that all we have to do is repent of our sin, and trust that he took our well-deserved punishment and separation from God. In believing he died for us, and choosing to live for him, we ourselves are crucified. We die. But he makes us alive again. That is why we must be reborn to enter the kingdom of heaven. He lives in us. And we live for him.
If you had the choice between a day on the cross, or eternity in hell, which would you choose?
Jesus' atonement for our sins was of infinite value, as he is an infinite being, who emptied himself out into nothing (
Philippians 2:7). So he doesn't need to spend an eternity in hell on our behalf. It was already paid in full through his sacrifice on the cross.
Now you might ask how is it fair for us to receive eternal separation for a single action or sin?
But isn't it fair to want eternal separation from your spouse because they cheated on you only once?
And transgressing against an infinite being has infinite consequences.
Another good C. S. Lewis quote is:
“There are only two kinds of people in the end: those who say to God, "Thy will be done," and those to whom God says, in the end, "Thy will be done." All that are in Hell, choose it. Without that self-choice there could be no Hell. No soul that seriously and constantly desires joy will ever miss it. Those who seek find. Those who knock it is opened.”