Question: how did xehanort time travel?
Answer: Ansem's function as a Heartless, a disembodied heart corrupted by darkness, enabled him to travel through time, just as Xehanort planned when he cast off his heart to create the former incarnation. ... Even after his initial defeat he remained within Riku for an extended period of time.