Gandalf was one of the Istari, mighty Maiar, sent by the Valar to 
Middle-Earth, when the first shadow of Sauron appeared there. Istari had bodies as of Men, and they could feel hunger and be slain, but "because of their noble spirits they did not die, and aged only by the cares and labors of many years" Unfinished Tales). Gandalf came the last of them and he seemed the least, for he was less tall than the others and looked more aged, grey-haired and grey-clad and leaning on a staff. But Cirdan, who saw further than the others in Middle-Earth, from their first meeting divined him the wisest and gave him the  Third Ring, Narya the Red. "Gandalf" was only his "by-name" among the people of the North of Middle-Earth, which meant "The Elf of Wand", for they thought him to be of the Elven-kind. But in Valinor his name was Olorin, from High-Elven "olo-s": fair vision, "fantasy" that Elves could make seem real, and he was of people of Manwe and Varda.
Most of all he loved the beauty of the fire, but he used his skill not for inspiring fear or awe, but for "mirth and delight". "Merry he could be, and yet quick at times to sharp speech the the rebuking of the folly; but he was not proud, and beloved among all those that were not themselves proud." Unfinished Tales) When orcs and other evil creatures began to gather in Dol Guldur, Gandalf went there and found out that Sauron arose again and was preparing to attack Lorien and Erebor. Owing to him Sauron's plans were partly ruined and Elven settlements in Lorien and on the North of Misty Mountains remained free. Gandalf was one of the main figures in the War of the Ring and the most dangerous enemy of Sauron. He was the first who found out about the Ring and only because of his awareness the Nazgul didn't manage to get it while it was still in Shire. He led the Fellowship from Rivendell and through the Moria, but when the pass was almost over, the company was attacked by Orcs and then Balrog. Gandalf fought with him on Durin's Bridge and casted him down, but Balrog pulled him from the bridge so they fell together, and everybody thought him dead. But they fell at the bottom and there Gandalf pursued Balrog until they reached the top of the peak of Celebdil and there Gandalf threw down his enemy, but was himself very weak: "Then darkness took me, and I strayed out of thought and time" so he said later, talking to Aragorn, Legolas and Gimli. Then he was "sent back for a brief time", until his task was done. He was brought to Lorien and healed there and clothed in white. He became "Saruman as he should have been" and yet stronger and wiser.
Then he helped Theoden in Rohan to beat off Saruman's invasion, visited him in Isengard and cast him off the White Council. After finishing his business with Saruman he rode to Minas Tirith and stayed there during its siege, and nobody except him and his hoarse Shadowfax could face the Nazgul. After the battle of Pellenor he was in the company that rode to Morranon to distract Sauron from the inner parts of Mordor, where the ring bearer supposed to be.
After the end of the War of the Ring he was King Elessar's
counselor for some time and in the autumn of the first year of the Fourth Age returned to the North with hobbits. There he visited Tom Bombadil and in the fourth year of the Fourth Age he returned to Valinor with the Wise of the previous Ages and the ring bearers - Frodo and Bilbo.