A: The principle in this is that Allah (Glorified and Exalted be He) is the Disposer of the affairs of His Creatures; He cannot be questioned as to what He does, while they will be questioned. To Allah belongs the creation and the command. A person has free will, but it is relative to the Will of Allah, as Allah stated,
To whomsoever among you who wills to walk straight.
And you cannot will unless (it be) that Allâh wills - the Lord of the ‘Alamîn (mankind, jinn and all that exists).
Allah has made clear the path of goodness and commanded us to follow it, and avoid the path of evil. He gave every person a mind to differentiate between good and evil. It is the person who chooses which way to follow, as Allah stated
Verily, We showed him the way, whether he be grateful or ungrateful.
(Part No. 3; Page No. 181) Allah also stated
And shown him the two ways (good and evil)?
Goodness which befalls a person comes from Allah, whereas any evil which afflicts a person comes from themselves, as Allah says,
Whatever of good reaches you, is from Allâh, but whatever of evil befalls you, is from yourself.
In conclusion, `Ibadah performed by a person, for which they are rewarded, is a gift from Allah to that person, which is performed by their own free will.
Sins committed by a person are also committed by their own free will, and Allah's punishment for them is the result of their own deeds, as they were the one who did it. Allah has thus treated them fairly. Everything is done by a person according to the Will and the Predestination of Allah. There is wisdom behind it all. Allah made this clear in His statement
"Wheresoever you may be, death will overtake you even if you are in fortresses built up strong and high!" And if some good reaches them, they say, "This is from Allâh," but if some evil befalls them, they say, "This is from you (O Muhammad صلى الله عليه وسلم)." Say: "All things are from Allâh."
May Allah grant us success. May peace and blessings be upon our Prophet Muhammad, his family, and Companions.