Below is an excellent and consise decription in laymen's terms of the Catholic teaching regarding prayer for the dead.
II Maccabees 12:43-46: "And making a gathering, he [Judas] sent twelve thousand drachms of silver to Jerusalem for sacrifice to be offered for the sins of the dead, thinking well and religiously concerning the resurrection, (For if he had not hoped that they that were slain should rise again, it would have seemed superfluous and vain to pray for the dead,) And because he considered that they who had fallen asleep with godliness, had great grace laid up for them. It is therefore a holy and wholesome thought to pray for the dead, that they may be loosed from sins."
Those who've died in a state of grace are not truly "dead"; they are our beloved in Heaven or in Purgatory (on their way to Heaven) and will forever be, world without end, part of the Communion of Saints -- the Church Triumphant (the Saints in Heaven, whether or not they are beatified or canonized), the Church Suffering (the saints in Purgatory), and the Church Militant (the saints on earth).
Because we can't know, aside from those the Church has beatified or canonized, who is already in Heaven, who is in Purgatory for a time, or who is damned, we pray for the dead for the rest of our lives -- assuming they are in Purgatory, while hoping they are in Heaven and not damned.
We also ask those who've died to pray for us. While those whom the Church has deemed to be of the Church Triumphant (the canonized Saints) are in Heaven for certain and are, therefore, in no need of our prayers for them, we've always asked for them to pray for us. As to the Church Suffering in Purgatory, Aquinas teaches that they are not able to know, by themselves, our prayers; however, it is piously believed, and taught by St. Alphonsus Liguori, that God makes our prayers known to them -- not directly, as they are deprived of the Beatific Vision until they enter Heaven, but by infusing this knowledge into their souls. St. Bellarmine teaches that because the Church Suffering is so close to God -- much closer than we are and having the great consolation of knowing they are saved -- their prayers for us are very effective. So, as you pray for your dead loved ones, ask them to pray for you, too!
As to the damned, there is no hope; no prayer can help them and we can't pray formally for those in Hell. The problem, of course, is that we can't know who is damned, and so we pray generally for "all the faithful departed." For those who've died outside of visible Communion with Christ's Church or for those Catholics who've died seemingly without repentance and in scandal, public prayer cannot be offered, but we can most certainly still pray privately with the hope that they've died in a state of grace (i.e., those who are denied a Catholic funeral can't be prayed for liturgically, publicly, but they can most definitely be prayed for -- and should be prayed for -- privately). Priests can even offer Masses for such people privately, without naming them.