Muslims believe that God knows everything that will happen, and has written it, and that people also have a real choice and are accountable for what they choose. The two are held together: God's knowledge does not force anyone to act.
The fuller answer
A simple way to see it: a teacher who knows his student will fail does not make him fail. God's knowledge is outside time, so He knows what each person will choose, and the choice remains theirs. The Quran says 'whoever wishes, let him believe, and whoever wishes, let him disbelieve', and also that nothing happens except by God's will.
In daily life, this belief gives a Muslim strength: take the means, work and plan, then accept what comes with trust and patience, without despair over a loss or arrogance over success. The Prophet ﷺ taught, 'Tie your camel, and then trust in God' (a hadith in at-Tirmidhi graded hasan by Al-Albani).
And say, "The truth is from your Lord, so whoever wills - let him believe; and whoever wills - let him disbelieve." Indeed, We have prepared for the wrongdoers a fire whose walls will surround them. And if they call for relief, they will be relieved with water like murky oil, which scalds [their] faces. Wretched is the drink, and evil is the resting place.
No disaster strikes upon the earth or among yourselves except that it is in a register before We bring it into being - indeed that, for Allah, is easy -
This answer follows the mainstream Sunni understanding and is general learning, not a fatwa. Where Muslims differ, the answer says so. Have a question of your own? Write to us.