The shows happen in the same universe, and the zombie apocalypse happened shortly after Luke and Leia were born. Lily, Marshal, Robin and Barney are all long dead. Ted and his family have survived for years, bunkered down in the basement of their house, which Ted was able to restructure for ultimate comfort, security and survival in the early days of the apocalypse, as he is an architect.
During the early days, a kind of mass hysteria manifests amongst some survivors. As a way of dealing with the trauma of what's happening, they pretend that everything is normal and fine. We saw Michonne do this with her family in the apartment in the flashback in Season 4. Ted and the Mother go and have dinner at the hotel where they met, and it isn't until the Mother's throwaway "What kind of mother wouldn't be at her own daughter's wedding?" that the illusion shatters momentarily, and they look at each other and tears fill their eyes as realise that life will never be the same again.
Ted has survived this long because of the Mother, and his friends. (because let's face it, without extreme assistance Ted would last, like, five minutes in the zombie apocalypse.) But as the years go by, everyone else has died or vanished. All that's kept him going is his love for the Mother and his family, and his very Ted-like determination that not only should they all survive, but that life maintain an appearance of beauty and magic. And because he's Ted, he somehow managed to achieve this.
Right before the pilot, Ted and the Mother ventured into the city to forage for more supplies. They were attacked by a mob of zombies, and the Mother was bitten. She begged Ted to mercy-kill her, and spare her a lingering agonising death before her children's eyes, followed by existence as one of the undead hordes. They share one final kiss, and then Ted raises the sword (he still has one of the swords from his and Marshall's apartment).
Ted stumbles back to his home and down into the basement. He's haggard, smeared in gore, and his eyes are red-rimmed and staring. Luke and Leia stare at him, wide-eyed.
Luke: "Dad, where's Mo—"
Ted: "Sit down, kids."
Luke and Leia sit down, slowly and carefully. Ted sits down opposite them. A long, long silence.
Ted: "Kids, I'm going to tell you an incredible story - the story of how I met your mother."
And so it begins. Ted begins to talk. And talk and talk and talk. All the flash-forwards we've seen in the show are flights of fantasy arising from Ted's PTSD, in which he imagines a world and a life where the zombie apocalypse hasn't happened. As the story goes on, he fantasises a happy ending for everybody, from Marshall, Lily, Robin and Barney, right down to Robin's annoying workmate Patrice from years ago.
Luke and Leia know that this is all a fantasy. But they also know that letting their dad tell this story is important, because it's the only thing that is keeping him sane. Hopefully.