It is true that Sita and her sons Lava and Kusha all stayed at the sage Valmiki's ashram (hermitage). But that is not how Valmiki came to know of the story of the Ramayana. He learnt the story earlier than that.
Once Valmiki was visited by the Devarishi (divine sage) Narada. Since Narada was knolwedgeable on a great many subjects, Valmiki asked him a question about whether there is any man in the world today who possesses the most illustrious qualities:
[T]he Sage-Poet Valmiki is inquisitively enquiring about a man who is a composite for all merited endowments in his form and calibre: "Who really is that person in this present world, who is principled and also a potential one, a conscientious one, a redeemer, and also a truth-teller and self-determined in his deed? Who is he conduct-wise blent with good-conduct. Who in respect of all beings is benign? Who is adept and also the ablest one [and] also uniquely good to look to? Who is that courageous one, who controlled his ire, who is brilliant, non-jealous and even whom do the gods fear, when provoked to war?"
And Narada told Valmiki that it's almost impossible for one man to have all these traits, but it turns out there is such a man living today, possessing these good qualities and so many more: the present king of Ayodhya, by the name of Rama. Narada gave a brief description of Rama's characteristics and life story, but Valmiki wanted to know more. Then Valmiki went to take a bath, when he saw a hunter killing a krouncha bird while its female companion was crying out in a mourning song, and so in anger Valmiki uttered the following sentence:
maa nishhaada pratiSThaamtva | magamaH shaashvatiiH samaaH ||
yat krauNcha mithunaat eka | mavadhiiH kaama mohitam||
"Oh! Ill-fated Hunter, by which reason you have killed one male bird of the couple, when it is in its lustful passion, thereby you will get an ever-lasting reputation for ages to come."
He was just absent-mindedly saying his words with the same rhythm that the Krouncha bird was crying out in, but then he realized the beauty of his sentence: it was a perfect line of poetic verse, the fist piece of poetry that humanity had ever composed. (The mantras of Vedas were already around, but that is poetry composed by the gods, not by Man.)
Valmiki then got home to his Ashram, and was visited by Brahma the creator god. Brahma explained that it was by his doing that Valmiki had uttered that sentence, and it was because he was blessing Valmiki with the ability to tell the entire life of Rama in poetic meter. Brahma said this:
You shall narrate the legend of Rama, the virtuous, intellectual and an intrepid one, and a godlike person in this world as well, as you have heard it from sage Narada. The adventures of valorous Rama along with Lakshmana, and the misadventures of demons, known or unknown in every detail, and even the plight of Vaidehi which is either revealed or un-revealed so far, and whatever legend that has happened, all that will also be known to you, even if it were to be unknown, as yet. You shall versify the heart pleasing and merit-yielding legend of Rama, and not a single word of yours will be unfounded in this epic. As long as the mountains and even rivers flourish on the surface of the earth, so long the legend of Ramayana will flourish in this world
So he gave Valmiki the boon that he would know the whole life of Rama, that everything Valmiki wrote in his epic poem would be accurate, and that his poem would endure forever. And sure enough, after Brahma disappeared and Valmiki started meditating, he saw the entire life of Rama:
Of Rama, Lakshmana and Seetha, also of King Dasharatha and his wives, and what bechanced on Rama when he was in kingdom Ayodhya; Valmiki veritably discerned all that. Their smiles, their conversations, their deeds and the succession of events as well, all of them the sage saw wholly and clearly by the yogic power conferred by Brahma.
And he saw not just Rama's past and present, but also what was yet to come in Rama's life:
The godly sage Valmiki composed the futuristic legend of Rama while on the surface of earth, and whatever that is there, that is composed in all its minuteness, in the coming chapters of this epic.
In any case, it was during Valmiki's composition of the Ramayana that Rama's wife Sita, by the path of destiny, came to Valmiki. And when Valmiki finished his epic poem, he taught it to Sita's sons Lava and Kusha. And Lava and Kusha spread this epic far and wide, until (through a serious of events described in the Uttara Kanda of the Ramayana) they came to the court of Ayodhya, and Rama himself asked the two boys to narrate the epic to him and his brothers:
Then those two singers, motivated by Rama's words, sang the ballad in maarga system, for they are well versed in it, then even Rama, who is also in the congregation, again to pacify his mind, [for the story of Seetha puts his mind to turmoil,] became interested at heart [to continue listening Seetha's story]
And that is how the narration of the Ramayana begins.