First off, the game title would be "Sonic and the Starry Knight", named after the main villain of the games plot.
The game's story is about a villain named the Starry Knight. The Starry Knight is a being that lives in the space between timelines. It usually lies dormant, sleeping for millions of years, but was woken up prematurely during the events of Sonic Generations by the Time Eater interfering with the timeline. Deeply enraged by its early awakening, it took to lay waste to the world that woke it up. During the pinnacle moment of the story, just before the showdown between the two Sonics and the Time Eater, just before the Sonics can go super, they are interrupted by the Starry Knight, who knocks them away from the Chaos Emeralds and consumes the Time Eater, taking control of the time-frozen world.
Sonic tries to stand up to the Starry Knight to protect his friends, and although Sonic stands no chance, the Starry Knight sees his act of heroism and decides to have mercy on them, in its own twisted way. It decides to give them a chance, by putting them through trials to hone their strength until they are powerful enough to face the Starry Knight. These trials are made up of pieces of Sonic's world and the different adventures he has gone on that have been pulled from both their timeline and even other timelines.