Kind of a lazy answer, but I'm not sure I can illustrate it any better... Have a look at this video about permutation tests and the documentation for the moran.mc() function from the spdep package in R—there an example that can be run at the bottom. I've included an additional document that might make clear what is happening in the permutation.