Gandalf is a good wizard who has powers beyond those of other characters: He sometimes uses a magic wand and he seems able to appear and disappear at will. He guides the travelers and rescues them from trolls, Goblins, and Wargs. He is old (he was a friend of Bilbo’s grandfather on the Took side) and respected for his wisdom. The map and key to the Lonely Mountain have been entrusted to him for safekeeping by Thorin’s grandfather. He also introduces Bilbo and the dwarves to useful friends like Elrond and Beorn.
Gandalf’s most important role in The Hobbit is as a tutelary or wisdom figure to Bilbo. He persuades Bilbo to go on the adventurous journey with the dwarves because he knows that Bilbo, without realizing it, is an essential player in the drama that will result in the death of Smaug. He also knows that the challenges of the journey are necessary to Bilbo’s further development; Bilbo has been safe and comfortable for too long. Thus Gandalf makes the secret mark on Bilbo’s front door that causes the dwarves to visit him in Chapter 1, and he supplies Bilbo with handkerchiefs and tobacco at the start of his journey so he will not have an excuse to turn back.
Gandalf is depicted at times observing Bilbo; you can infer that he is judging his behavior. When Bilbo emerges from Gollum’s cave, he does not tell the dwarves about the ring of invisibility, but he catches Gandalf looking at him knowingly. It is shortly after Gandalf leaves the group at the edge of Mirkwood in Chapter 7 that Bilbo begins to experience his newfound courage and cleverness; he even assumes Gandalf’s function of rescuer when he frees the dwarves from the dungeon of the Elvenking. Gandalf has functioned as a parent and teacher to Bilbo, and an indication of Bilbo’s growth is that he does not need the physical presence of Gandalf to guide him in the palace of the Elvenking or in Smaug’s lair.
After the Battle of Five Armies, Gandalf accompanies Bilbo back to the hobbit-lands. The very last scene depicts Gandalf and Balin visiting Bilbo some time later in his hobbit-hole. Gandalf is represented in all his wisdom, reminding Bilbo of his place in the grand scheme of things.