Someone recently commented on Reddit that while it’s easy to assume that Gandalf the White had some sort of prescience or foreknowledge of events, that was almost certainly not Tolkien’s intent while writing The Lord of the Rings.
If you read some of Tolkien’s writings, you will find him using the word estel. This is a word of his own invention, spoken in the Elvish tongues of Quenya and Sindarin. The closest English translation would be faith.
If I were to sum up the meaning of estel, I would say that it embodies the belief that there is no darkness so dark that the light cannot penetrate.
Gandalf had no foreknowledge of how the events of the War of the Ring would play out, but he did have estel, and I would say had more than most.