In Appendix B to The Return of the King, the August 3018 entry briefly mentions how Gollum hid in Moria to evade both the Elves and the servants of Sauron who hunted him, and how he was unable to leave through the West-Gate (the gate through which the Fellowship entered) when he finally found it.
The background material published as "The Hunt for the Ring" in Unfinished Tales goes into a bit more detail. Gollum entered Moria not only to hide from his enemies — particularly from the Ringwraiths, who were searching for the Shire along the Great River — but also in hope of crossing the Misty Mountains westward in order to find the Shire and the Ring himself.
What then happened to Gollum cannot of course be known for certain. He was peculiarly fitted to survive in such straits, though at the cost of great misery; but he was in great peril of discovery by the servant of Sauron that lurked in Moria, especially since such bare necessity of food as he must have he could only get by thieving dangerously. [...] he became lost, and it was a very long time before he found his way about. It thus seems probable that he had not long made his way toward the West-Gate when the Nine Walkers arrived. He knew nothing, of course, about the action of the doors. To him they would seem huge and immovable; and though they had no lock or bar and opened outwards to a thrust, he did not discover that. In any case, he was now far away from any source of food, for the Orcs were mostly in the East End of Moria, and was become weak and desperate, so that even if he had known all about the doors he still could not have thrust them open. It was thus a piece of singular good fortune for Gollum that the Nine Walkers arrived when they did.
Still in the vicinity of the gate, Gollum quickly picked up the Company's trail; Frodo already hears his footsteps before they make their first stop for sleep several hours after entering the Mines (as confirmed by the entry for January 13, 3019 in "The Tale of the Years": "Attack by Wolves in the early hours. The Company reaches the West-Gate at nightfall. Gollum begins to trail the Ringbearer"). The passage quoted above suggests that Gollum, by then desperate to get out, would have followed anybody. But it didn't take him long to feel or guess that his Precious was with the party: he somehow found his way out of Moria despite the attack on the Fellowship and later that same day dared to enter the (to him) terrible Elvish land of Lothlórien. The idea of escape had quickly given way to pursuit of the Ring.
How did he know the Ring was with the Company? I discount the idea that he could sense it as the Ringwraiths could: during the Riddle Game Bilbo stood right in front of him with the Ring in his pocket, yet at its end Gollum failed to guess it was in there in 4 tries. But the sight of Gandalf, who had quizzed him extensively about the Precious, leading a party that included no less than 4 hobbits might have been a good hint.
In the end, though, I think Gollum must have perceived a resemblance between Frodo and Bilbo. He wasn't in any doubt as to which hobbit had the Ring: he tried to climb the tree Frodo is in the first night in Lórien, and later he unerringly followed Frodo when the Fellowship broke above Rauros Falls. Gollum's ability to track them seems to have been based mainly on scent (in the Emyn Muil, Frodo says "In this dry bleak land we can't leave many footprints, nor much scent, even for his snuffling nose"). However much or little Bilbo and Frodo looked alike, it could well be that they smelt enough alike to Gollum. I would also note that, besides the Ring, Frodo carried 2 of Bilbo's other belongings. Bilbo had never worn the mithril coat all that much and had left it in a museum for decades, so I don't see how it could have carried much of his scent by the time of Frodo's journey to Mordor. But Bilbo carried Sting and its shabby leather scabbard hidden inside his trousers from the time he got it in the trolls' lair early in The Hobbit until he returned to Bag End at the end of the story; and it never left his possession until he gave it to Frodo less than 3 weeks before the Company entered Moria.