AFAIK it all boils down to the fact that during embryonal development our cells, which at that point were just a blob of undifferentiated autonomous chemical machines, somehow managed to unanimously agree upon the cardinal directions (up-down, left-right, front-back) for future development - and thanks to this, we don’t have toes growing out of our ears.
They don’t so much know where they are as much as they’re just interacting with what’s there in the way they were made to, which was all put there due to the DNA to function as a whole.
And to be clear, it’s all still amazing, just in a different way. Here’s Kurzgesagt video that helps explain how these cells “communicate” to do things like put a body together in the right order.
Here is an alternative Piped link(s): https://piped.video/watch?v=TYPFenJQciw
Piped is a privacy-respecting open-source alternative frontend to YouTube.
I’m open-source, check me out at GitHub.