When you die, you will find yourself on an escalator. It will be either the down escalator or the up escalator. After death you will not have a chance to choose which escalator you prefer; that choice you already will have made in life, and it is a choice that becomes irrevocable at death.
You will find yourself on the down escalator if you die graceless, in the state of mortal sin. The down escalator has no intermediate landing. Once on it, you are deposited quickly at the bottom and will be among those who, like you, have chosen creature over Creator.
On the other hand, if you die repentant and in the state of sanctifying grace, you will find yourself on the up escalator. Unlike the down escalator, this one has an intermediate landing. Most people who take the up escalator exit on that landing temporarily. You probably will be among their number. The reason is found in Revelation 21:27, which says that "nothing unclean" shall enter heaven. Although bound for heaven because you have died in the state of grace, you likely will not be thoroughly spotless at death. Some uncleanliness will remain, some attachment to sin, no matter how minor.
Before expiring, you were absolved. You were sorry for your sins, particularly the larger and more obvious ones, but you nevertheless still had residual affection for some sins, at least for some of the venial ones. Your love for God was good but not perfect. Your soul was in the state of grace, but not perfectly so. You were clean, on the whole, but a few smudges remained, making you not quite fit to take the escalator all the way to the top. Thus you stop at the intermediate landing, a way station where you will remain as long as needed to clean up those moral smudges. We call this intermediate landing purgatory. No one stays on there forever, but people stay on it for differing and indeterminate periods of time. (I use "time" loosely here. Properly speaking, the afterlife is outside of time.)
In purgatory—the word means "place of cleansing"—the dead experience both God’s mercy and his justice. They experience his mercy in that, while he owes no one salvation, they are on their way to heaven. They experience his justice in that they receive exactly what they deserve in terms of punishment for their (repented-of) sins. Frank Sheed observed that "sorrow for sin, if it is deep and real, involves suffering." Some of that suffering for repented-of sin occurs here below, before death. The rest of it comes after death, in purgatory.
A few people—we call them saints—end their lives perfected through their sorrow for sin and through the sufferings they have endured and have offered back to God. At death they too get on the up escalator, but for them there is no intermediate landing. They get off at the very top to hear "Well done, good and faithful servant!" That is something we too will hear, after leaving the intermediate landing and returning to the escalator. Our journey will have been a bit longer, but the destination will be the same.