How can the sun go down in minecraft when the world is infinitely large and flat?
Minecraft is a game that was programmed accordingly.
If the world is infinite, how can it have a maximum height?
In Minecraft, infinite means something like "too big as that even a halfway normal player would ever reach the end if it wasn't laid out on it", since it would be quite awkward to write it down as it was abbreviated to infinite.
Since the Minecraft world is not infinitely large (think +/- 32000 is the end), the sun and moon also move along the underside.
The end of the world:
Just because the world is infinite or virtually infinite does not automatically mean that the whole world is loaded into the memory and displayed at the same time. The skybox in which the sun goes down is always further away than the part of the world that the game is currently showing. Thus, the sun and moon can rise and set again as normal.
Minecraft maps are not infinite. It is a large area that you move around and not a planet simulator.
A minecraft world definitely doesn't end at the barrier. You can simply teleport behind it and advance further or you could also expand the barrier… There are also many videos on yt in which it is also shown that the world is theoretically infinite, but the game actually crashes at some point.
The maximum size of the world is 2 ^ 64 * 2 ^ 64 blocks
Where is that
A minecraft world definitely doesn't end at the barrier. You can just teleport yourself behind it and advance further or you could also widen the barrier
In the cited link I understand that the barrier is at 29.xxx, you can teleport behind it but hardly anything there, you can still walk up to 3x.xxx but then it's over.
And I read from there that the "Far Lands" - that is, the theoretically endless world behind the barrier, was removed at 1.8 and has since ended at 3x.xxx.