Here's how:
* The trolls: The trolls, Tom, Bert, and William, capture the dwarves and Bilbo in their cave.
* Bilbo's cunning: Bilbo, using his quick thinking and the distraction of the trolls arguing over who gets to eat him, manages to steal the troll's magic key.
* The sun's power: Bilbo uses the key to unlock the door of the troll's cave, allowing the sunlight to stream in. This turns the trolls into stone.
So, while the Company was trapped, it was Bilbo who ultimately saved them.