Gandalf was known as one of the Istari, a Maiar or one of an "angelic" order. The Maiar were 'spirits' who were supposed to have been able to self incarnate or take human form. Saruman was the chief and was higher in the Valinorean structure than the others.
The Maiar/Istari were sent by the Valar at a crucial time in Middle-earth and their aim was to enhance the resistance of the Elves and the Men of the West to the evil that was growing in the east. Thus it was that Gandalf having discovered the evil that was emanating from Dol Guldur in Mirkwood, felt grave concern that Sauron was planning to attack Lorien and Rivendell. There was no one in the North left to resist any attempt of Sauron to gain control of the northern passes in the mountains. Moreover, Gandalf was extremely worried that Sauron would use the dragon, Smaug, to achieve his aims.
In 2941, Gandalf made the decision to visit The Shire for respite from his dark thoughts. By doing so, he felt he might be able to come up with a way of dealing with the situation of growing evil. Just as he was nearing Bree however, he encountered Thorin Oakenshield.
What has been learnt about Thorin so far.
1. As a young lad he was witness to the invasion of his home, Erebor, and the massacre of many of his kin.
2. . He was courageous and a good fighter as has been seen in the battle of Azanulbizar where he gained his name "Oakenshied"
3. He lived most of his formative years in exile with his father and grandfather.
4. He lost both his grandfather and father in tragic circumstances. Both had been killed attempting to reclaim their heritage, birthright and wealth which had been taken from them.
5. When Thorin became heir of Durin's line after the death of is father, he was still relatively young for a dwarf. He was a dwarf in exile but despite this, he managed to build a home for his people in Ered Luin, west of Eriador. Many of Durin's folk who were wanderers learned of Thorin's halls and decided to join him. His people prospered and were comfortable but they continued to remember and sing of the good days of Erebor.
6. Thorin was hard working, and traded successfully with the people of Eriador. He was considered a "great dwarf of proud bearing". He never forgot his noble heritage and became more dissatisfied with his peoples' situation in exile as the years went by. He felt a strong sense of injustice on behalf of his people, and a sense of duty to restore them to the Lonely Mountain by defeating Smaug, the dragon. Through this defeat, he would reclaim Erebor and the treasure of his forefathers; and avenge the wrongs brought on his House.
"The years lengthened. The embers in the heart of Thorin grew hot again, as he brooded on the wrongs of his House and the vengeance upon the Dragon that he had inherited. He thought of weapons and armies and alliances, as his great hammer rang in his forge; but the armies were dispersed and the alliances broken and the axes of his people were few; and a great anger without hope burned him as he smote the red iron on the anvil." J.R.R. Tolkien: "Return of the King Appendix A: III: Durin's Folk" p 358.
On March 15th 2941, Thorin who was returning home from a journey, met Gandalf by chance in Bree. The story evolves into the "Quest for Erebor".