You can sometimes understand a proof and its origin if you summarise it fast, like so. There's a topology on $\mathbb{Z}$ whose open sets are unions of arithmetic progressions; then open sets are nonempty or finite so $S:=\{n\in\mathbb{Z} \mid \not\exists p\in\mathbb{P}:p|n\}=\{-1,\,1\}$ isn't one of them, and each AP is also closed; with finitely many primes, $\{-1,\,1\}$ would be open. OK, so what triggers such thoughts?
It's funny that, while the contents of $S$ as defined above are trivial to work out, the set is defined as a complement with respect to the union of APs whose contents are unknowable without working out the primes. But as soon as you notice this role of unions of APs, you can't help but wonder whether they have any interesting properties that unions of sets do, such as those topology discuss. And since "unions of open sets are open" isn't open$\mapsto$closed-true unless you add "finitely many" into it, it's natural to explore whether the infinitude of the primes comes into this, especially when you notice that the topology you're now thinking of is not only valid, but one in which your open sets are also closed.
As to why it worked out in the end, sadly sometimes the answer is just "when I tried it, it did".