Gandalf is not a "wizard" in the classical fantasy sense of the word, one whose power and wisdom is learned in dusty towers poring over old books. He is a divine being, one of the Maiar, whose very essence is wisdom. Indeed, it can be interpreted that the Ainur (the Maiar and the Valar) are all aspects of the universal divinity, each embodying some quality:
[Eru, the Creator] made first the Ainur, the Holy Ones, that were the offspring of his thought, [...] But for a long while they sang only each alone, or but few together, while the rest hearkened; for each comprehended only that part of the mind of Ilúvatar from which he came, and in the understanding of their brethren they grew but slowly. Yet ever as they listened they came to deeper understanding, and increased in unison and harmony.
(From the Ainulindalë, the Music of the Ainur, in the Silmarillion. Emphasis mine).
So some of Gandalf's wisdom is inborn, a part of his essence. But some he learned from other Maiar or the Valar themselves:
Wisest of the Maiar was Olórin [=Gandalf's name in Valinor]. He too dwelt in Lórien, but his ways took him often to the house of Nienna, and of her he learned pity and patience.
(From the Valaquenta, in the Silmarillion).