If God knows everything we have done and will do then how do we have free will?

Firstly, let’s go over some fundamental points of prior understanding. Human beings are the best of Allah’s creation (if they obey him) whom He blessed with intellect and free will. He gave us the ability to observe, and comprehend the world around us, and to ponder upon the real purpose of creation, as mentioned in the Qur’an.

Indeed, in the creation of the heavens and earth, and the alternation of the night and the day, and the [great] ships which sail through the sea with that which benefits people, and what Allah has sent down from the heavens of rain, giving life thereby to the earth after its lifelessness and dispersing therein every [kind of] moving creature, and [His] directing of the winds and the clouds controlled between the heaven and the earth are signs for a people who use reason. Surah Baqarah, chapter 2, verse 164

God created mankind for the sole purpose of worshipping Him alone. He gave us intellect for us to contemplate on His greatness through all the Blessings He provided us with. He wants us to recognize His favors upon us and be in His servitude unconditionally, but He did not impose His worship on us. He gave us choice for us to think about and understand the reality of this life and then believe in Him of our own volition. At the same time, He sent prophets (peace be upon them) to remind us what the truth of this life is so that we may be guided towards God and will have no excuse in front of Him on the day of judgment that we were unaware of our true purpose of existence. It is mentioned in the Qur’an:

[We sent] messengers as bringers of good tidings and warners so that mankind will have no argument against Allah after the messengers. And ever is Allah Exalted in Might and Wise.4, 165. 

Being our Creator, He has absolute knowledge of all matters of the present, past and future. Not only does Allah know what will be, but He also has full control and power over what He allows to come into existence. He can allow or deny anything from ever occurring. God’s knowledge encompasses everything that occurs in the heavens and the earth and whatever is hidden in the darkest places of the oceans and the layers of the earth. 

“Indeed, Allah [alone] has knowledge of the Hour and sends down the rain and knows what is in the wombs. And no soul perceives what it will earn tomorrow, and no soul perceives in what land it will die. Indeed, Allah is Knowing and All-Aware”. Surah Luqman, chapter 31, verse 34.

Now coming back to the question: if Allah has all power over everything and He already knows everything we will do then where is the free will? 

 Firstly, one must understand here that knowledge does not translate into coercion or persuasion. It is a fact that Allah’s knowledge encompasses everything and He is above and beyond the confines of space and time. He has complete control over all things and nothing happens outside His will in the universe. He knows everything that lies in the future, what is in the present and what has been done in the past, and He is all-aware of what we will choose from the beginning, but that doesn’t imply that God forces us to do what is in His prior knowledge.

The choices we make are based on our own free will which He knows through His all-encompassing knowledge. Whatever we choose, God makes us the recipient of our choice – whether we choose to be believers or disbelievers in God; to be truthful or dishonest people; to benefit society or harm it and so on. However, the choices that we make will only materialize by Allah’s permission as everything is under His control and He has the power to stop us or allow us to go ahead with our choices according to His wisdom. Just because you choose something doesn’t mean that God will accept it and produce it. That is for Him to decide. There is a prophetic narration that says:

Narrated 'Umar bin Al-Khattab: I heard Allah's Messenger saying, "The reward of deeds depends upon the intentions and every person will get the reward according to what he has intended. So, whoever emigrated for worldly benefits or for a woman to marry, his emigration was for what he emigrated for." Sahih al-Bukhari 1.

This narration clearly says that a person is accountable for his actions based on his intentions and he will be rewarded for his deeds accordingly. Whatever one intends is by their own will.  Allah also states in the Qur’an:   

Each soul is responsible for its own actions; no soul will bear the burden of another. You will all return to your Lord in the end, and He will tell you the truth about your differences. Surah An’am 6; verse, 164.

This verse explicitly states that everyone will be responsible for his actions. If there would be no free will, how would one be accountable? With choice comes accountability, otherwise there would be no purpose of the day of judgment. It is mentioned in the Qur’an;

“We have certainly created man in the best of stature; Then We return him to the lowest of the low, Except for those who believe and do righteous deeds, for they will have a reward uninterrupted.  So, what yet causes you to deny the Recompense?  “Is not Allah the most just of judges?” Surah Teen, chapter, 95, verse,4- 8.

What we know on this subject from the knowledge provided in the Qur’an and the prophetic traditions is that humans are granted free will or choice to choose the path of success or failure which will determine their future accordingly in the next life. Allah knows best. May Allah guide us.