The frying pan was Gollum, and the fire was the wargs.
The expression “out of the frying pan and into the fire” means out of one bad situation and into a worse one. It you think about it, you are in a bad way if you are in the frying pan, but once you are in the fire you are not in the pan, which is good, but you are now in the fire, which is bad and in some ways worse.
The frying pan in this situation was Gollum. Bilbo was in a difficult situation because he went into Gollum’s cave, and had to play the riddle game to get out. Gollum decides that Bilbo needs to have a competition with him.
“… If precious asks, and it doesn't answer, we eats it, my preciousss. If it asks us, and we doesn't answer, then we does what it wants, eh? We shows it the way out, yes!" (Ch. 5)
Bilbo has to keep Gollum guessing with riddles, and has to guess the answers to Gollum’s riddles. It is a very stressful situation, and he is playing for his life. In the end, he cheats when he asks Gollum what is in his pocket and goes along when Gollum thinks it is a riddle and cannot guess. Bilbo gets out, putting the ring on and getting away.
When Bilbo escapes, he finds his friends again and the next things he knows all of them are running away from wargs and hiding in trees. They went up the tree because the wolves could not climb the tree. Wargs are scary wolflike creatures. They were not there by accident.
The Wargs and the goblins often helped one another in wicked deeds. Goblins do not usually venture very far from their mountains, unless they are driven out and are looking for new homes, or are marching to war … (Ch. 6)
Fortunately, the eagles came to rescue them, because Gandalf has friends in high places. The giant eagles are able to carry them off to safety, getting them out of the frying pan for good and temporarily safe.