Think of it like this:
If N is not prime, then there is a tuple (p, q) | 2 <= p < N & 2 <= q < N.
p <= q when we begin testing. Each iteration of the loop we increment p by 1. As a result, q necessarily has to decrease, because N/p = q.
When p > q during the iteration process, then for each iteration there and after, the only whole numbers q can be -- if N is not prime -- have already been tested, so we would be doing double the work for no reason, thus we stop at that point.
Disclaimer: My ability to speak mathematics has diminished over the years, so I might not be as clear or concise as I could be. For this I apologize, but I hope it helped.