Medieval Catholic priests invented Purgatory. It's not mentioned in Scripture, but it was a useful idea. What's so attractive about the idea of Purgatory that the Catholic Church invented the idea and has taught it as doctrine for centuries?
Christians get into Heaven on the basis of their faith in Jesus Christ. This inconvenient fact left medieval Catholic priests in a quandary. They had a hard time threatening lay people with punishment after death. If you can't control congregants by threatening them with pain in the afterlife, you've got one strike against you. So the priests invented Purgatory (see also, Sacraments). "It's true that you'll get to Heaven even if you disobey me," the priests told their flocks, "but you'll suffer for hundreds of year in Purgatory first." Thus, the myth of Purgatory increased the power that the priests had over their congregations.
The myth of Purgatory also satisfied the vengeful wishes of the people at large. Those who believed in Heaven wanted to think that their mean uncles, cruel step-mothers, etc., were suffering in Purgatory before enjoying eternal life in Heaven. Jesus' radical message of salvation is hard for people to take because it isn't "fair," which is to say, it benefits people we don't like.
The myth of Purgatory also satisfied the urge among the people to be holier than others. It was disturbing to think that one's sinful neighbors would get into Heaven just like oneself. One wanted to be judged more worthy of Heaven. One wanted to be among the blessed few whose years in Purgatory are short. One wanted to be special in the eyes of God. Jesus' message of radical equality before God denied people's natural desire to be better than their neighbors—to love them, if that's the game plan, but to feel superior to them as well.
Purgatory played to people's desire to be worthy of Heaven. Eternal life as a free gift from a loving God is all very well and good, but it doesn't boost one's pride to receive a boon that one doesn't deserve. Purgatory gave people a way to believe that they would earn worthiness.
Purgatory, along with the Catholic doctrine of "particular judgment" of the individual at death, also ties in to Plato's pagan myth of reincarnation. Plato depicted people being judged individually at death, rewarded or punished, and then reincarnated. Particular judgment makes sense in a system that's on an indefinite time scale. It can go on forever. The idea that you get your reward (for good or ill) immediately after death is well established in popular culture, but it's very different from the New Testament account of judgment as universal and happening at "the end of the world." This idea of Judgment Day, in turn, comes from Zoroastrianism, the fertile religion that also brought us the Prince of Darkness and the virgin birth.
Catholic scholars point to references in the early church to prayers for the dead, as if these demonstrate that the doctrine of purgatory was around in the first centuries of Christianity. They say that souls who can benefit from prayer can’t be in heaven or hell, so they must be in purgatory. In fact, these prayers reflect the early Christian (and 1st-century Jewish) belief that the dead in hades rested either in peace or torment while awaiting Judgment Day (see the story of Lazarus and the rich man). One needs purgatory to explain these beliefs only once one has bought into Plato's teaching that the dead are sent to their fates immediately after death. If early Christians had believed that the dead were sent to their respective rewards immediately after death, you’d see it in the Nicene Creed. But the Nicene Creed touts “the resurrection of the dead,” not rewards after death.
The Pope has said that Purgatory isn't a place after all but a state of being. Whatever that means.
PS: The medieval Catholics invented Purgatory as we know it but not out of thin air. The concept dates back to Rome's Virgil, Plutarch, and Orphic mysticism. In the Middle Ages, Virgil was taken to be a saint and magician, so maybe his reference to a fiery subterranean purgatory was the basis for the 12th century development of the myth. Purgatory is another case in which the Roman Catholics picked up useful material from pagan Roman religion, along with the Pontifex Maximus and revering statues of the divine dead.