The Enduring Friendship
Gandalf and Bilbo's friendship was always touching in The Lord of the Rings, but Gandalf's canon history makes their connection even better. The pair are seen to be close in The Lord of the Rings: The Fellowship of the Ring, smoking pipeweed together and reminiscing about the good old days, and they wound up sailing away together to the Undying Lands at the end of The Lord of the Rings: The Return of the King. They were drastically different characters, but they were very dear friends who both played significant parts in the salvation of Middle-earth—and they had far more in common than it would seem.
Gandalf Smoking Pipe Beside Bilbo In Battle Of The Five Armies (1)
Gandalf first met Bilbo when the Hobbit was a young boy, and he initially noticed him for his particular interest in his stories about grand adventures. This allowed Bilbo to come to mind when Gandalf was in need of a burglar to help Thorin and co. in The Hobbit. Of course, as Bilbo became older, he got far too comfortable with his life at the Shire. He tried to ignore his desire for adventure, so Gandalf had to provide significant motivation to get Bilbo going. As the old wizard said in The Lord of the Rings: The Fellowship of the Ring, he had to give the Hobbit a 'nudge out the door.' Coincidentally, this isn't too different from the start of Gandalf's journey to Middle-earth.
Gandalf's Backstory and Similarities with Bilbo
Gandalf's canon backstory reveals that, like Bilbo, he hadn't always been willing to head out on adventures. The old wizard is actually an ancient being called a Maia, and he was created before the universe itself. His true name was Olórin, and leading up to the Second Age of Middle-earth, he lived in Valinor, studying and serving under the god-like Valar. When Sauron's presence in Middle-earth began to grow, the Valar determined that a few Maiar (called the Istari) should head out to guide the Elves and Men in their fight. Among those chosen was Olórin, but he was very reluctant to leave the safety and security of his home.
Like Bilbo, Gandalf was too afraid to set out into the world and, therefore, needed a nudge from a wiser being. The Valar knew that he was the right man for the job, even if Gandalf didn't yet know it himself. This is precisely the same as how the wizard felt about Bilbo. He knew the little Hobbit was capable of great things and provided a similar nudge as the Valar had given him (though the Valar didn't send Dwarves to pester Gandalf). Ultimately, this parallel reveals why Gandalf and Bilbo worked so well as friends, adding another heartwarming layer to the kinship that existed between them.
The Satisfying Endings
Gandalf's past not only retroactively makes his friendship with Bilbo more heartwarming but also makes the two characters' endings even more satisfying. At the end of The Lord of the Rings: The Return of the King, Bilbo and Gandalf set sail together for the Undying Lands (Valinor). This is, of course, where Gandalf was from, and this is his first time returning since the Valar sent him to Middle-earth. With their similar stories, it's sweet to think of them arriving to this paradise together and living out the rest of Bilbo's days as the dearest of friends.
Undying Lands Ship Lord of the Rings (1)
It's fun to imagine that the old wizard would have returned to his original form after he returned to Valinor. This would have been quite a surprise for the Hobbits traveling with him, but it would have brought more touching resolution to their stories. Bilbo finally seeing Gandalf, or Olórin, as he truly was would have brought them even closer as friends. Though it's implied that Valinor had no pipeweed since this was a vice Gandalf discovered during his time in the Shire, it can be hoped that the pair could have ended their Lord of the Rings story together blowing smoke rings, fully understanding and appreciating how their stories had always been so intertwined.