Both the Qur’an and Sunnah call believers to supplicate Almighty Allah and implore Him in all circumstances. Furthermore, supplication is regarded in Islam as an act of worship for which one is rewarded, whether it is answered or not. Besides, Almighty Allah may have decreed that supplication be a means for something taking place or not. In other words, Almighty Allah may destine that if one supplicates Him for something, He will grant one so and so or protect one against so and so of evil things and vice versa.
giving more detailed explanation on this, Dr. Safar Ibn `Abdur-Rahman Al-Hawali, said:
There are two points of view on this issue. The first reflects upon Almighty Allah’s words: (Allah effaces what He wills, and establishes (what He wills), and with Him is the source of ordinance.) (Ar-Ra`d 13: 39)
According to this view, the ‘source of ordinance’ is inscribed in a Tablet preserved with Almighty Allah where there is no change or alteration of Allah’s destiny for people. But there are two other kinds of divine ordinances for Almighty Allah’s creation; one takes place yearly on the Night of Decree or Lailatul-qadr for all creation, and the other occurs per one’s age, that is, when the soul is infused in one while one is still a fetus in the mother’s womb.
A certain thing may be definitely destined for one in the Preserved Tablet but is contingent upon offering supplication in one or both of the other kinds of ordinances. This is to say, for example, that Almighty Allah may have destined in the yearly or per-age ordinances that if one supplicates Him to cure a certain disease, for instance, He Almighty will grant this, and if one does not supplicate Him Most High in this regard, the disease will remain. This same applies to what is written in the Preserved Tablet but with definite ordinance, that is, one may in this situation either supplicate Almighty Allah and so be cured or not do so and thus remain afflicted with the disease.
We do not know what Almighty Allah has actually destined for us. This is to motivate us to always supplicate Him the Almighty to grant us good things and ward off bad ones from us. In this regard we are to bear in mind that if we supplicate Allah Almighty, He the Most High will reward us any way, either by granting us what we want or deferring it until He compensates us for it with better things in this world or in the Hereafter.
The second point of view on the question at hand also supports the above one. It says that supplication is a means for the occurrence or non-occurrence of something. To illustrate this, if a person, for example, drives his car at night while he feels sleepy, he may have an accident; the means to ward off this is to avoid driving while one feels sleepy. Likewise, learning to swim is a means that helps one avoid the danger of drowning. By analogy, supplication is a means that Allah Almighty has made lawful for us so that we may, through offering it to Him the Most High, ward off the occurrence of unfortunate things we anticipate to take place in our lives.
There are many examples where people in distress have supplicated Almighty Allah to deliver them from the affliction they suffer from and He the Most High has answered them. This is proved to be true even with disbelievers when they, upon afflicted with hardships, resort to their Creator.
People do not know whether a hardship would afflict them or not, but on anticipating it, they by instinct resort to Almighty Allah in humble supplications so that He the Almighty may deliver them from it.
Based on the above, Muslims are required to always supplicate Almighty Allah in all circumstances, especially in cases when they are exposed to hardship.