In The Name of Allaah, The Most Merciful, The Bestower of Mercy.
Alaah [The Most High] said:
وَعَسَىٰٓ أَن تَكۡرَهُواْ شَيۡـًٔ۬ا وَهُوَ خَيۡرٌ۬ لَّڪُمۡۖ وَعَسَىٰٓ أَن تُحِبُّواْ شَيۡـًٔ۬ا وَهُوَ شَرٌّ۬ لَّكُمۡۗ وَٱللَّهُ يَعۡلَمُ وَأَنتُمۡ لَا تَعۡلَمُونَ
And it may be that you dislike a thing which is good for you and that you like a thing which is bad for you. Allah knows but you do not know.” [AI-Baqarah. Verse 216]
Imaam Ibnul Qayyim [may Allaah have mercy upon him] said:
There is for the servant -in this verse – a number of insightful guidelines, underlying benefits and welfare, because indeed if he knows that Al-Makrooh (what is hated) can bring forth Al-Mahboob (i.e. what is beloved), and vice versa, then neither would he feel safe from the harm that might occur from something that makes him happy nor would he lose hope whilst expecting a final source of happiness from a situation of harm. That is because he does not have (infinite or perfect) knowledge of the Awaaqib (i.e. the final outcomes), but Allaah knows that which he does not know.
There is nothing more beneficial for him than fulfilling Allaah’s commands, even if it is difficult for him in the beginning and his soul dislikes it, because all its end result will be good – a means to happiness, pleasure and joy. Likewise, there is nothing more harmful for him than doing what he has been forbidden, even if his soul desires and inclines towards it, because indeed all its end result will lead to pain, grief, evil and calamities. A distinguishing characteristic of (sound) intellect is that it prefers to bear little pain whose end results will lead to great enjoyment and abundant good, and it avoids that little enjoyment whose end results will lead to great pain and prolonged evil. The observations of an ignorant person does not permit him to (pay attention to the true or real) goals behind events that occur from the very beginning of an affair, but as for the sensible person, he always looks at the (true or real) goals behind those events. He looks at the praiseworthy and unpraiseworthy goals that are not obvious (to the ignorant one). He sees what is forbidden as tasty food that is mixed with deadly poison, therefore, whenever he is urged towards eating that food due to its tastiness, he is turned away due to the poison in it. As for the commandments, he sees them as bitter medication that will lead to physical well-being and cure; therefore, whenever he is turned away from the medication due to its bitterness, he is then urged towards it due to its benefits.
However, this requires the blessing of knowledge by way of which a person can perceive the (praiseworthy and unpraiseworthy goals) behind those events that occur from the very beginning of an affair, as well as firm patience that would enable him to bear the difficulties upon the path towards achieving the expected goals. If he does not have certainty and patience, it would be difficult for him to achieve that; but if he has firm certainty and patience, he would be facilitated with ease whilst bearing every difficulty in his pursuit of everlasting good and enjoyment. [Source: An Excerpt from ‘Al-Fawaa’id’ pages 203-204. slightly paraphrased]