From the beginning, Christians have prayed for the dead and have undertaken works of penance on their behalf. There is scriptural basis for this intercessory prayer for the sins of others and for the dead in the Old Testament. Job's sacrifices purified his sons (Job 1:5); and Judas Maccabeus "made atonement for the dead that they be delivered from their sin" (II Macc 12:46).
The tradition in the Church of having Masses said for the dead began in the earliest times. The pre-Christian Roman religion, which held that some form of life continued after death, gave votive offerings to the gods for the dead at three specified times: the third, seventh and thirtieth day after death. This practice of praying for the departed on these same days was adopted ("inculturated") by the early Christians -- and continued in the Church for nearly 2000 years: the Church offered Masses for the deceased person on the third, seventh and thirtieth day after death.
Beginning in the year 998, All souls -- the "faithful departed" -- were officially remembered in the Church's prayers on the evening of November 1, and with Requiem Masses, Masses for the dead, on November 2. All Souls Day is now a feast of the universal Church. (The word "requiem" is Latin for "rest".) Following the Second Vatican Council, all Masses celebrated on All Saints day observe that feast, not "All souls". Three Masses may still be said on All Souls Day. The first two are Masses for Burial, and the third is a Mass for the Dead. Black vestments may be worn on this day.
We pray for the faithful departed, those who have been baptized, but who need to be completely purified of all stain of sin before they come into full union with God in Heaven. In other words, most of us. The Church's teaching about Purgatory, the place of purification, is explained in the Catechism of the Catholic Church (§1030-1032):
"All who die in god's grace and friendship, but still imperfectly purified, are indeed assured of their eternal salvation; but after death they undergo purification so as to achieve the holiness necessary to enter heaven.
"The Church gives the name Purgatory to this final purification of the elect, which is entirely different from the punishment of the damned. The Church formulated her doctrine of faith on Purgatory especially at the Councils of Florence and Trent. The tradition of the Church, by reference to certain texts of Scripture, speaks of a cleansing fire:
"As for certain lesser faults, we must believe that, before the Final Judgment, there is a purifying fire. He who is truth says that whoever utters blasphemy against the Holy spirit will be pardoned neither in this age nor in the age to come. From this sentence we understand that certain offenses can be forgiven in this age, but certain others in the age to come.
"This teaching is also based on the practice of prayer for the dead, already mentioned in Sacred Scripture: 'Therefore [Judas Maccabeus' made atonement for the dead, that they might be delivered from their sin.' From the beginning the Church has honored the memory of the dead and offered prayers in suffrage for them, above all the Eucharistic sacrifice, so that, thus purified, they may attain the beatific vision of God. The Church also commends almogiving, indulgences, and works of penance undertaken on behalf of the dead:
"Let us help and commemorate them. If Job's sons were purified by their father's sacrifice, why would we doubt that our offerings for the dead bring them some consolation? Let us not hesitate to help those who have died and to offer our prayers for them." [Saint John Chrysostom - 4th century]