In Appendix B to "The Return of the King", the August 3018 entry briefly mentions how <PERSON> 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. <PERSON> entered Moria not only to hide from his enemies -- particularly from the <PERSON>, 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 <PERSON> 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 <PERSON> that the Nine Walkers arrived when they did.
Stil in the vicinity of the gate, <PERSON> quickly picked up the Company's trail; <PERSON> 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. 381
The Company reaches the West-Gate at nightfall. <PERSON> begins to trail the Ringbearer"). The passage quoted above suggests that <PERSON>, 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 Lothlorien. The idea of escape had quickly given way to pursuit of the <PERSON>.
How did he know the Ring was with the Company? I discount the idea that he could sense it as the <PERSON> could: during the Riddle Game <PERSON> stood right in front of him with the <PERSON> in his pocket, yet at its end <PERSON> failed to guess it was in there in 4 tries. But the sight of <PERSON>, 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 <PERSON> must have perceived a resemblance between <PERSON> and <PERSON>. He wasn't in any doubt as to which hobbit had the Ring: he tried to climb the tree <PERSON> is in the first night in Lorien, and later he unerringly followed <PERSON> when the Fellowship broke above Rauros Falls. <PERSON>'s ability to track them seems to have been based mainly on scent (in the Emyn Muil, <PERSON> says "In this dry bleak land we can't leave many footprints, nor much scent, even for his snuffling nose"). However much or little <PERSON> and <PERSON> looked alike, it could well be that they smelt enough alike to <PERSON>. I would also note that, besides the Ring, <PERSON> carried 2 of <PERSON>'s other belongings. <PERSON> 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 <PERSON>'s journey to Mordor. 381