Temporal punishment (purgatory) is necessary to reconcile God's justice and mercy.
On the one hand, nothing defiled can enter heaven. Even a venial sin requires atonement. And forgiven sins require restitution. Would it be just for a murderer to repent and be forgiven by God and not have to do anything to atone for his sin (either in this life or in the next)?
What if it was your son that he murdered? I'm sure you'd see the need for the repentant sinner to do some kind of prayers, fasting, good works to make up for his sin. Such actions are also the natural, spontaneous fruits of true repentance (as St. John the Baptist pointed out).
Like the analogy of the neighbor forgiving the boy for breaking a window with his baseball -- but nevertheless requiring him to pay for a new window. It is only just.
But if there were no purgatory, then a soul could be condemned to eternal Hell for one venial sin. Considering the weakness of our frame, that would be a real problem.
That's where God's mercy comes in. God is perfect justice AND perfect mercy.