You use the notion of a logarithm to extract exponent, since $\log(a^n)=n\log(a)$. First we wish to isolate the $(1+r)^{-n}$ algebraically to get
$$1-\frac{rPV}{PMT}=(1+r)^{-n}$$ and then we apply the logarithm to get
$$\log\left(1-\frac{rPV}{PMT}\right)=-n\log(1+r)$$ and so we wind up at
$$n=-\frac{\log\left(1-\frac{rPV}{PMT}\right)}{\log(1+r)}$$