Immediately after death, after the soul leaves the body, a person is judged based on his or her life in the body. If the person dies in friendship with God, that means, in a state of sanctifying grace, which conveys the life of the Blessed Trinity to the believer, but not completely loving God, he or she is incapable of the vision of God and must undergo a purification of the person's love for God. This recalls the great remark of St. John of the Cross, "At the end you will be judged on your love. Strive to love as God desires to be loved and abandon your present state." God has indicated how He desires to be loved in the Great Commandment: You must love the Lord your God with all your heart, all your mind, all your strength. The Book of Revelation 21:27 states: " Nothing impure will ever enter it…" it being the vision of God, known as the Beatific Vision.
Prayer for the dead goes back to the Jewish people and the early church practiced prayer for the dead indicated by inscriptions on the tombs of Christians in the catacombs. The Book of Maccabes points out: "Thus he (Judas Maccabes) made atonement for the dead that they might be absolved from their sin." 2 Maccabes 12:46 Their sin, soldiers dead in battle, was the sin of idolatry. The Councils of Lyon (1274), Florence (1438) and Trent (25 Session, 1563) taught with clarity the meaning of prayer for the dead, their purification, hence, the name Purgatory. But what is this purification?
All sin has two consequences, it disturbs our relation with God and leaves a wound. Our relation with God can be completely severed called mortal sin, spiritual death, or weakened called venial sin. These sins leave a wound even after being forgiven for them, and that is why the believer receives a penance in Confession to make up for the wounds of one's sins and voluntary penance is for that purpose and can be made for the sins wounding the Church not only for our own sins but the sins of others. This is the great message of the devotion to the Sacred Heart of Jesus and the Blessed Virgin at Lourdes, Fatima, and so many of her appearances. In fact there is a whole encyclical letter, The Most Merciful Redeemer, on the meaning of expiation for sin which can be accessed here.
The wound of sin is the disorder in our own lives and the contributing to the disorder of sin in the world or put another way our loves are out of order. We continue to be attracted to a love or loves opposed to God's love. This purification either takes place here on earth or in the next life. The purifying pain of purification is different from the pain of the damned in Hell but it is real pain and the pain is basically this: the person is deprived of the vision of God which he or she now yearns for but cannot be admitted to without shedding the baggage of the wound of sin brought upon leaving this earth. This causes intense suffering but the good kind of suffering because the person is being healed and drawn to eternal happiness Who is Love Himself, God. Popes, clergy, religious, kings, great leaders, all kinds of people can be in a state of purification even souls who at the last minute repent their sins and seek forgiveness from God.
When the Blessed Virgin appeared to the children in Fatima in 1917, Lucia asked the Blessed Virgin about the eternal fate of friends of hers who died. When she asked about one friend, Amelia, our Lady told Lucia that Amelia would be in Purgatory until the end of the world. Of course "end of the world" is a phrase that measures time according to this changeable world. Time is a measurement, a measurement of change. However when we leave this world and enter the next, time in that sense ceases. Nevertheless when a person is being purified there is a certain change in that a potential increase in love of God is being actualized. The great Catholic layman, Frank Sheed, explains a difficult way of looking at this called "aeveternity" and the meaning of time in eternity in his book Theology and Sanity:
Aeviternity is the proper sphere of every created spirit, and therefore of the human soul. But the soul's special relation to the matter of the human body gives it a necessary and proper relation to continuous time (which is the body's duration) which other spirits are not troubled by. At death, this distracting relation to matter's time ceases to affect the soul, so that it can experience its proper aeviternity. But during this life, time presses upon the soul, if only by way of the heartbeats that never cease. The soul can become too much immersed in matter, in the limitations of time and space and change. Love of change is a disease that the soul contracts from the body? one sure symptom of it is the inability to contemplate. During contemplation, time really does stand still for the soul, which is one reason why we should practice it: for it means practicing the soul in its own proper element.
So time is very much measured in terms of change of our bodies (aging) and material realities. But spiritual realities are different and so the notion of time with respect to spirits is different. Frank Sheed asks us to consider the angels:
But what of spirit? Because it knows change at all, even if only accidental change, it is not in eternity; but because the changes it knows are not continuous, it is not exactly in time, either. The spirit does indeed know a before and after. If God gives an angel a particular revelation, for instance, then something in the angelic mind is aware of his state when he did not have the revelation and his state when he has it. But there is nothing in the nature of spirit that requires these changes; they happen when they happen, they do not bring change into his nature itself; and in between, the spirit rests in the changeless possession of what he has. His NOW is more closely akin to the abiding now of eternity than to the flowing now of time. For his duration, too, there is a word—the word aevum or AEVETERNITY, the duration of that which in its essence or substance knows no change: though by its accidents it can know change, and to that extent is in time too, but a sort of discontinuous time, not the ever-flowing time of matter.
(NOTE: essence or substance is what something is by nature but accidents are something which come and go in a nature but do not change it. All human beings have the same human nature but differ in accidental qualities: height, shape, age etc.) Aeviternity is not easy to understand but it helps us comprehend what goes in the purification of the human person in the afterlife in answer to the question how long?
The people in purgatory are washed by the Blood shed for them by Christ on the Cross and made present for us in every Mass. Only God knows the exact moment the person is now ready to see Him. Think of baptism. At baptism we received a beautiful spotless robe but during life this robe became discolored and damaged. There is this beautiful description in the Book of Revelation chapter seven:
14 Then one of the elders asked me, "These in white robes—who are they, and where did they come from?"I answered, "Sir, you know."And he said, "These are they who have come out of the great tribulation; they have washed their robes and made them white in the blood of the Lamb.