According to Tolkien mythology, there is a sort of Hierarchy of Spirits in terms of gods and goddesses who created and shaped the world. Ilúvatar, or Eru, was the almighty creator, while the Ainur, which comprised of the Valar and Maiar, were angel-like beings. The Valar were greater than the Maiar, but all were created by Eru before the world came into existence. Gandalf is one such Maiar. And each Maiar has a Valar as a sort of mentor or master. Gandalf, or Olórin, how he was known before coming to Middle Earth, was an apprentice of Irmo, who was the lord of visions and dreams. But he was also a disciple of Nienna, who was the goddess of sorrow, pity and courage. It is said that those who listen to her learn wisdom and endurance in grief. In the Silmarillion, it’s stated that Olórin was responsible for giving the elves fair visions and putting wisdom into their hearts.
When Gandalf was chosen by the Valar to go to Middle Earth and challenge Sauron, he said that he was too weak and frightened to challenge him. Manve, the greatest of the Valar, said to him that this is the exact reason why he was chosen and why he should go. To aid him in his task, Cirdan, lord of the Grey Havens, gives Gandalf Narya, the ring of fire, on his arrival to Middle Earth. Cirdan said to him that the ring can be used to “rekindle hearts in a world that goes chill”. Gandalf the Grey wasn’t the only wizard appointed to this task. The order was comprised of five such wizards, among which are also Saruman the White and Radagast the Brown. There were also two blue wizards, but not much is known about them. The last mention about these two is that they traveled east, never to be seen again. Nevertheless, by the end of the story, only Gandalf remained true to his original mission.