Your question brings up a perpetual argument between free will and predestination, That question has been argued for decades that I am aware of and probably even Centuries.
The only answer that can possibly deal with this is that God is eternal. What that really means is that there is no such thing as time as far as God is concerned. There is no yesterday and no tomorrow.
Revelation 22:13 I am Alpha and Omega, the beginning and the end, the first and the last.
So what can we determine from this Scripture? This actually says that all things are lumped together in him, or in other words to God there is no difference between the fist day of creation and the last day in Revelation.
Without time all things are simultaneous. To God all of the centuries of time are like a flash card that we see for only a short time and yet we are aware of all that was on the flash card. To God he constantly is aware of all things without end in what we call the past and what we call the future.
Just as a writer already has an idea of how to end a book when beginning the first chapter, so God planned the end as he initiated the creation.
The only way to answer your question is to say:
Isaiah 55:8 and 9 For my thoughts are not your thoughts, neither are your ways my ways, saith the LORD. 9 For as the heavens are higher than the earth, so are my ways higher than your ways, and my thoughts than your thoughts.
hope this helps.