From Bible Doctrine (Note from Randy Alcorn: I highly recommend this book, which is a condensation of Grudem’s larger Systematic Theology, which I also recommend enthusiastically)
We must begin by recognizing that as long as God is God and we are his creatures, there must be some unanswered prayers. This is because God keeps hidden his own wise plans for the future, and even though people pray, many events will not come about until the time that God has decreed. The Jews prayed for centuries for the Messiah to come, and rightly so, but it was not until “the time had fully come” that “God sent forth his Son” (Gal. 4:4). The souls of martyrs in heaven, free from sin, cry out for God to judge the earth (Rev. 6:10), but God does not immediately answer; rather, he tells them “to rest a little longer” (Rev. 6:11). It is clear that there can be long periods of delay during which prayers go unanswered because the people praying do not know God’s wise timing.
Prayer will also be unanswered because we do not always know how to pray as we ought (Rom. 8:26), we do not always pray according to God’s will (James 4:3), and we do not always ask in faith (James 1:6-8). And sometimes we think that one solution is best, but God has a better plan, even to fulfill his purpose through suffering and hardship. Joseph no doubt prayed earnestly to be rescued from the pit and from being carried off into slavery in Egypt (Gen. 37:23-36), but many years later he found how in all of these events “God meant it for good” (Gen. 50:20).
When we face unanswered prayer, we join the company of Jesus, who prayed, “Father, if you are willing, remove this cup from me; nevertheless not my will, but yours, be done” (Luke 22:42). We join also the company of Paul, who asked the Lord “three times” that his thorn in the flesh be removed, but it was not; rather the Lord told him, “My grace is sufficient for you, for my power is made perfect in weakness” (2 Cor. 12:8-9). When prayer remains unanswered, we must continue to trust God, who “causes all things to work together for good” (Rom. 8:23 NASB) and to cast our cares on him, knowing that he continually cares for us (1 Peter 5:7). We must keep remembering that he will give strength sufficient for each day (Deut. 33:25) and that he has promised, “I will never fail you nor forsake you” (Heb. 13:5; cf. Rom. 8:35-39).
We also must continue to pray. Sometimes an answer, long awaited, will suddenly be given, as it was when Hannah after many years bore a child (1 Sam. 1:19-20), or when Simeon saw with his own eyes the long-expected Messiah come to the temple (Luke 2:25-35).
But sometimes prayers will remain unanswered in this life. At times, God will answer those prayers after the believer dies. At other times he will not, but even then, the faith expressed in those prayers, and their heartfelt expressions of love for God and for the people he has made, will still ascend as a pleasing incense before God’s throne (Rev. 5:8; 8:3-4), and will result in “praise and glory and honor at the revelation of Jesus Christ” (1 Peter 1:7).