"After the Myths Went Home", a short story by Robert Silverberg; first published in The Magazine of Fantasy and Science Fiction, November 1969, available at the Internet Archive. Any of these covers look familiar?
Here is a summary from Majipoor.com:
More than ten thousand years into the future, people bore easily, so they start recreating great historical figures for their amusement. When they tire of that, they start making fictional and mythical characters. At their distant remove, it's pretty much the same thing. So they make Adam and Eve, Pan, Odysseus, the Minotaur, Salome, and many others. But eventually the people became bored with the myths, too. It is only later that they discover that having myths is maybe a good thing.
It ends with all the people being put back in the machine and sent back
Leor had provided us with a splendid diversion. But we all agreed it was time for the myths to go home. We had had them with us for fifty years, and that was
We rounded them up, and started to put them back into the machine. The heroes were the easiest to catch, for all their strength. We hired Loki to trick them into returning to the Hall of Man. "Mighty tasks await you there," he told them, and they hurried thence to show their valor. Loki led them into the machine and scurried out, and Leor sent them away, Heracles, Achilles, Hector, Perseus, Cuchulainn, and the rest of that energetic breed.
and the last one to go in is Loki who says one day they will regret it because people need to believe in something.
That was Cassandra, not Loki:
Year after year the task of finding and capturing them continued, and one day we knew we had them all. The last to go was Cassandra, who had been living alone in a distant island, clad in rags.
"Why did you send for us?" she asked. "And, having sent, why do you ship us away?"
"The game is over," I said to her. "We will turn now to other sports."
"You should have kept us," Cassandra said. "People who have no myths of their own would do well to borrow those of others, and not just as sport. Who will comfort your souls in the dark times ahead? Who will guide your spirits when the suffering begins? Who will explain the woe that will befall you? Woe! Woe!"
"The woes of Earth," I said gently, "lie in Earth's past. We need no myths."
Cassandra smiled and stepped into the machine. And was gone.
It ends with aliens conquering the world.
And then the age of fire and turmoil opened, for when the myths went home, the invaders came, bursting from the sky. And our towers toppled and our moons fell.
And the cold-eyed strangers went among us, doing as they wished. And those of us who survived cried out to the old gods, the vanished heroes.
Achilles, defend us!
Shiva, release us!
Heracles! Thor! Gawain!
But the gods are silent, and the heroes do not come. The machine that glittered in the Hall of Man is broken. Leor its maker is gone from this world. Jackals run through our gardens, and our masters stride in our streets, and we
are made slaves. And we are alone beneath the frightful sky. And we are alone.