Here is Charles Spurgeon's able answer to such a difficult question:
"Another objection has been raised that is very ancient indeed, and it has a great appearance of force. It is raised not so much by skeptics as by those who hold a part of the truth. It is this: prayer can certainly produce no results because the decrees of God have settled everything and those decrees are immutable. Now, we have no desire to deny the assertion that the decrees of God have settled all events. Certainly, it is our full belief that God has foreknown and predestinated everything that happens in heaven above or in the earth beneath. I fully believe that the foreknown station of a reed by the river is as fixed as the station of a king, and the chaff from the hand of the winnower is steered as the stars in their courses. Predestination embraces the great and the little; it reaches to all things. The question is, why pray? Might it not as logically be asked, Why breathe, eat, move, or do anything? We have an answer that satisfies us; namely, our prayers are in the predestination, and God has as much ordained His people’s prayers as anything else. So, when we pray, we are producing links in the chain of ordained facts. Destiny decrees that I should pray—I pray. Destiny decrees that I will be answered—the answer comes to me.
But we have a better answer than all this. Our Lord Jesus Christ comes forward, and He says to us, “My dear children, the decrees of God need not trouble you; there is nothing in them inconsistent with your prayers being heard. ‘I say unto you, Ask, and it shall be given you.’
Now, who is the One who that says this? Why, it is He who has been with the Father from the beginning: “The same was in the beginning with God” (John 1:2). He knows what the purposes of the Father are and what the heart of the Father is, for He has told us in another place, “The Father himself loveth you” (John 16:27). Now, since He knows the decrees of the Father and the heart of the Father, He can tell us with the absolute certainty of an eyewitness that there is nothing in the eternal purposes in conflict with this truth, that he who asks receives and he who seeks finds. He has read the decrees from beginning to end. Has He not taken the book, loosed the seven seals thereof (Rev. 5:5), and declared the ordinances of heaven? He tells you there is nothing there inconsistent with your bended knee and streaming eye and with the Father’s opening the windows of heaven to shower upon you the blessings that you seek.
Moreover, the One who promises to answer prayer is God Himself. The purposes of heaven are His own purposes. He who ordained the purpose here gives the assurance that there is nothing in it to prevent the efficacy of prayer. “I say unto you.” You who believe in Him, your doubts are scattered to the winds; you know that He hears prayer."
-Charles Spurgeon, The Power in Prayer