It's rather confusing due to time travel stuff but I think when Sora is shown in his station of awakening after using the power of waking to go to the past and save Kairi, he's supposed to be in some kind of atemporal bubble, from where he's able to go to different points in time to find clues about how to save Kairi. He was just a heart after all, and so was Naminé (after the heartless tornado "killed" Kairi), and we know (because of brown-robed Xehanort's heartless) that disembodied hearts are able to trascend time. That's why they were able to met there, and maybe because of Naminé's special powers. So I think that's the Naminé from the past, moments before past Sora arrived to The Final World, that's why time-traveling Sora tells her to go to the world of ocean and sky, so his past self meets her there (you know, so that part of the past timeline doesn't change) and she can reunite with Kairi once past Sora reverts time for the first time (lol, that's confusing). Naminé says that she is trying to reach Terra's consciousness and talk to him, and in The Final World she says she spoke to him, so it makes sense.
It's not that she's in his heart at all, she was just "visiting" in a metaphysical way (thanks to her connection to Sora) like KeybladeLordSora said. Similar to Roxas and Kairi connecting at the beginning of KH2.
As for when she returns to Kairi, there are two possibilities:
1) If ReMind Naminé comes from the point in time when the shadow tornado killed Kairi (I tend to believe this), she returned to Kairi when Sora left The Final World, saved the Guardians of Light and found Kairi before returning to the Keyblade Graveyard and back in time. I need to point out that Naminé says she still has not been able to return to Kairi when they are witnessing Terra's consciousness fight Terra-Xehanort which happens a bit AFTER returning to the Keyblade Graveyard (and I said she returned to Kairi before that). It seems like a contradiction, but it's not because Sora and Naminé are in a rip of time which is not part of the timeline at all.
2) If Naminé comes from the future just after Xehanort killed Kairi, she returned to her when Sora reconstructed Kairi's heart and brought her back.
The very fact that Naminé says she's trying to talk to Terra and Sora tells her to go to the world of ocean and sky is what makes me think that ReMind Naminé doesn't come from the future, because it was the Naminé that got separated from Kairi after the shadow tornado who did those things.