There are three ways this question usually comes up. The first is related to the Catholic belief in Purgatory. The second refers to a misunderstanding of Jesus' proclamation in Sheol between His death and resurrection. The third is more personal and more ambiguous.
Does the Bible condone praying for the dead?
The Roman Catholic Church teaches that some Christ-followers who have died will spend time in a holding place, Purgatory, before they can enter heaven. These are people who died having committed venial sins that were not sufficiently atoned for. Roman Catholics are encouraged to pray for those in Purgatory so that their suffering will be eased and their time shortened. Since Purgatory does not actually exist, it is foolish to pray for anyone affiliated with it.
The second case is when a believer prays that an unbeliever who has died will accept Christ in the afterlife. Unfortunately, this is not possible. People do not receive a second chance to repent after they have died. If they did, the rich man in Luke 16:19-31 would have asked forgiveness for himself, not his brothers. And Hebrews 9:27 agrees, saying, "just as it is appointed for man to die once, and after that comes judgment." But 1 Peter 3:18-19 adds confusion to the argument. It says that between the crucifixion and the resurrection, Jesus "proclaimed to the spirits in prison." The prison in question is generally regarded to be the holding place for demons who sinned so egregiously God removed them from earth. The word translated "preach" means to herald a message. Jesus announced that He had conquered Satan and his forces, but He did not offer life to people who had already died rejecting Him.
The final situation that most often brings up the question of praying for the dead is related to retroactive petitionary prayer—a subcategory of "backward causation." Is it appropriate to pray for the salvation of someone who has died under ambiguous spiritual circumstances? If we didn't know if he was saved or not, could we pray retroactively that he was? Since the outcome is unclear to the survivors, and since God lives outside of our time, it would make sense that we could ask God to have worked in the life of someone who has died. Such a prayer could also be offered regarding physical comfort, for example, to pray that someone's sudden death was painless.
Of retroactive prayers, C.S. Lewis says:
When we are praying about the result, say, of a battle or a medical consultation, the thought will often cross our minds that (if only we knew it) the event is already decided one way or the other. I believe this to be no good reason for ceasing our prayers. The event certainly has been decided—in a sense it was decided "before all world." But one of the things taken into account in deciding it, and therefore one of the things that really causes it to happen, may be this very prayer that we are now offering. Thus, shocking as it may sound, I conclude that we can at noon become part causes of an event occurring at ten A.M.
What he is saying is that in the time between your cancer test and the call from the doctor with the results, you can still pray that the test is negative. The cancer is there or not at that moment, but since God is not constrained by linear time, your prayer may still influence whether or not you have it. For the argument at hand, the same may be said. Either the friend died instantly or slowly, either he was saved or not. But our prayer may yet have influence.
Logically, the argument is sound. But while it brings up an interesting intellectual exercise, there is no indication in the Bible that such prayers would be retroactively effective. If they would, it would be reasonable to see some kind of instruction in the Bible, but the Bible never mentions praying for things that have already passed. It is true that God exists outside of our timeline, but when He made us, He confined us to linear time. All indication is that He wants us to concentrate on the present (Matthew 6:34).
So, the Bible tells us there is no Purgatory and there is no second chance at salvation after death; it follows that it is useless to pray for the dead as related to these topics. But the Bible does not mention if we should or shouldn't pray for things that have already happened. Still, it makes more sense to pray about the present and the people in need now, than the people who have already passed.
What happens after death?
Is salvation possible after death? Is there a second chance for salvation?
Is the concept of purgatory biblical?
Death - What does the Bible say about it?
What is the relationship between God and time?