We discover that Gandalf has re-entered the narrative after leaving the dwarves at Beorn's house when Bilbo decides to steal the Arkenstone and take it as bargaining power to Bard and the Elves to prevent a war. When he goes to the Elvenking and Bard, Gandalf reveals himself, and although he does not tell Bilbo straight away why he returned, we discover later that he clearly had heard of the amassing of the armies of the Goblins and the Wargs. Gandalf chooses to reveal this news to prevent a confrontation between the Dwarves and the elves and men:
"Halt!" he called in a voice like thunder, and his staff blased forth with a flash like the lightning. "Dread has come upon you all! Alas! it has come more swiftly than I guessed. The Goblins are upon you! Bolg of the North is coming, O Dain! whose father you slew in Moria. Behold! the bats are above his army like a sea of locusts. They ride upon wolves and Wargs are in their train!"
With this news Gandalf is able to divert the animosity and unite the Elves, men and Dwarves in the Battle of the Five Armies.