"There have been many allegorical explanations to answer this question, but we may point to more familiar examples from contemporary science.
A clinical psychologist performing an experiment may ask certain questions of his subjects and, although he is quite sure of what the answer will be, knowing his subject's personality, he will refrain from exerting any influence.
Hence the answer, even if known beforehand by the scientist, is freely chosen and therefore valid in terms of the experiment.
In terms of Divine providence, however, what is the purpose of testing someone if the results are already known?
A medieval Sage, the Ramban, has ventured the opinion that it is necessary in order for an action to emerge from the realm of the potential to the manifest; a person has to earn the reward for a good deed and not for a good intention.
The trials and tests a man is made to undergo are not meant to provide God with information that He already knows.
They are meant to help a person realize the greater potential in him, to reinforce his capacity to overcome difficulties and to create something new.
It is a way of letting spiritual powers become expressed in practice.
The emphasis is not on God's knowing but on knowing as a human experience."
–Rabbi Adin Steinsaltz
From The Candle of God, p. 201, by Rabbi Adin Steinsaltz