Scripture does not teach that God will always give the believer what they ask for in prayer. But it does teach things about prayer that are in significant tension with the experience of unanswered prayer — and the tension is real and should not be resolved too quickly.
Jesus's teaching in Matthew 7:7–8 is among the most direct:
Matthew 7:7–8
ESVAsk, and it will be given to you; seek, and you will find; knock, and it will be opened to you. For everyone who asks receives, and the one who seeks finds, and to the one who knocks it will be opened.
This is unqualified. It does not say "sometimes it will be given." It says everyone who asks receives. The person who has prayed desperately and not received what they asked for has a right to feel the tension between this promise and their experience.
Paul's experience with his "thorn in the flesh" (2 Corinthians 12:7–9) is the most important biblical precedent for unanswered prayer. Paul prayed three times that the thorn — whatever it was — would be removed. God did not remove it. God's response was: "My grace is sufficient for you, for my power is made perfect in weakness." The thorn remained. Paul received something other than what he asked for — not the removal of the difficulty but a revelation about grace and power operating within it.
Several things are worth noting about this passage. First, God answered Paul — he just did not answer in the way Paul wanted. Prayer is not a mechanism for controlling God's actions; it is a relationship in which God responds. Sometimes the response is "yes," sometimes "not yet," and sometimes "something better." Second, the answer Paul received was not an explanation of why the thorn was allowed — it was an encounter with God's sufficiency in the midst of it. Third, Paul describes the thorn as "a messenger of Satan" — not as something God designed for Paul's good in a straightforward sense, but as something God permitted and then redeemed.
Jesus himself prayed a prayer that was not answered in the way he asked for. In Gethsemane: "My Father, if it be possible, let this cup pass from me; nevertheless, not as I will, but as you will" (Matthew 26:39). The cup did not pass. Jesus went to the cross. The prayer was answered — but the answer was not the removal of suffering.