Purgatory is the Roman Catholic doctrine on the fate of souls that are in God's saving grace at the time of death and have perfect assurance of salvation but which require, as a matter of divine justice, remaining temporal punishment for forgiven minor sins ("venial" sins) or major sins ("mortal" sins) that were forgiven prior to death. These souls are believed to undergo a final purging after death, so that the believer who is cleansed from them will then enter Heaven where "there shall in no wise enter into it any thing that defileth" (Rev 21:27). The punishment for sin is twofold: eternal and temporal. The judgment for unforgiven and unrepented sins is hell. When sin is forgiven, the eternal punishment is entirely removed, along with some, or most, or all of the temporal punishment according to the wisdom of a loving God who is both merciful and just. The doctrine of purgatory was formulated at the Councils of Florence and Trent, based on Biblical exegesis.
Purgatory is mentioned in the Bible as a "Prison" in a few places like Mat 5:25 and 1 Pet 3:19.  Some theologians cite 1 Corinthians 3:13-15 as a scriptural basis for it; also the example of David 2 Samuel 12:13-23. The argument is (1) Bad Christians, whose works are bad or sinful, have their works burned up and suffer loss; these will still be saved, but only through fire, which is Purgatory; while, (2) Good Christians, who have worked well with faith in Christ, receive a reward from the Lord on Judgment Day, just as He promised many times in the Gospel (e.g. Mark 9:41). Seen in this sense, 1 Cor 3:15 is considered to be Scriptural evidence of both Purgatory and merits, and good works for sanctification post-justification. Good works don't cause our salvation, but they are necessary for salvation to be effected by the Lord.
"Another related proof comes from Our Lord saying some sins are forgiven in this world, implying some others could be forgiven in the world to come (Mt. 12:32) ... To the question “Whether there be any Fire of Purgatory in the next life,” the saintly pope answers in the affirmative, saying, “We must believe that before the day of judgment there is a Purgatory fire for certain small sins: because our Saviour saith, that he which speaketh blasphemy against the holy Ghost, that it shall not be forgiven him, neither in this world, nor in the world to come. Out of which sentence we learn, that some sins are forgiven in this world, and some other may be pardoned in the next: for that which is denied concerning one sin, is consequently understood to be granted touching some other" Pope St. Gregory and other Saints interpreted Mat 12:32 as implying the existence of Purgatory.
Those whose transgressions are too great to ever warrant redemption, and those who refuse to repent of sins they have confessed, don't go to Purgatory, but are instead sent straight to Hell after death for all their unconfessed mortal sins. In brief, mortal sins cause hell, while venial sins cause Purgatory.