The Nakomoto consensus should work regardless of where the miners are located, as long as they are using the same network. The internet on earth, as it currently stands, is fully connected. So miners in China, Germany, and the United States are all contributing to the network where information is travelling across the internet.
It could be conceivable that different planets (or the moon) would have their own internet at some point, disconnected from that of earth’s, but that seems less likely than being connected via satellite transmission. As long as the networks of the moon and Mars are connected to the internet on earth, everything should still work normally.
Edit: Thanks to @PeterWuille in the comments for correcting this information. Here’s what was said:
“Nakamoto consensus requires that the block propagation time between miners is negligible compared to the inter-block time. If it isn’t, miners which are latency-wise “close” to each other will find more blocks that proportional to their hashrate share (simply because further out miners have a delay before being able to start working on a block), resulting in a (geographical) centralization pressure. When the latency exceeds the inter-block time (like it would be between Earth and Mars) the networks will split and fail to converge even.”