First, if $n\equiv 1\mod4$, it can't be a U.F.D., because it is not the integral closure of $\mathbf Z$ in $\mathbf Q(\sqrt n)$, and a U.F.D. is integrally closed.
Second, when it is the integral closure, it is equivalent to say it is a P.I.D.
Starck-Heegner's theorem gives the $9$ values of $n<0$ for which the ring of integers is principal, which are all congruent to $1$, except $-1$ and $-2$.
For a positive $n$, one doesn't even know if there is an infinite number of them (*Gauß' conjecture). In each case one may study the group of ideal classes, which is trivial if and only if the ring is principal. One way of showing it is not is to find two different decompositions of an element as a product of irreducible elements.