First of all, if $ p \equiv 1 \pmod{4} $ then $ \mathbf Z[\sqrt{p}] $ is not even integrally closed in its field of fractions: it does not contain $ (1 + \sqrt{p})/2 $, which is certainly in $ \mathbf Q(\sqrt{p}) $, and it is an algebraic integer since it is a root of
$$ x^2 - x - \frac{p-1}{4} $$
Since any unique factorization domain is integrally closed, we conclude that $ \mathbf Z[\sqrt{p}] $ is not a unique factorization domain.
If $ p \equiv 3 \pmod{4} $ then $ \mathbf Z[\sqrt{p}] $ is the ring of integers of $ K = \mathbf Q(\sqrt{p}) $, thus the question comes down to computing the class number of the number field $ K $. The square-free $ d $ for which $ \mathbf Q(\sqrt{-d}) $ has trivial ideal class group are known: they are
$$ d = 1,\, 2,\, 3,\, 7,\, 11,\, 19,\, 43,\, 67,\, 163 $$
For a general algorithm to compute whether $ \mathcal O_K $ is a UFD/PID, we can use the Minkowski bound. The ideal class group $ G $ of $ K $ is generated by prime ideals of norm smaller than $ M_K $, where
$$ M_K = \frac{2^{s+1}}{\pi^s} \sqrt{|p|} $$
with $ s = 0 $ if $ p > 0 $ and $ s = 2 $ otherwise. If the ideal class group is trivial, all such prime ideals will be principal. Therefore, it suffices to show that the primes with $ q < M_K $ are either inert, or there is an element $ x \in \mathcal O_K $ with $ N(x) = \pm q $. Quadratic reciprocity tells us when a prime $ q $ is inert: it is precisely when $ T^2 - p $ is irreducible in $ \mathbf Z_q[x] $, in other words, it is precisely when $ p $ is not a quadratic residue modulo $ q $.
Here are two concrete examples: let's apply this algorithm to $ \mathbf Z[\sqrt{-5}] $ and $ \mathbf Z[\sqrt{7}] $. For $ \mathbf Z[\sqrt{-5}] $, the Minkowski bound is $ M_K \approx 3.6 $, so it suffices to look at the primes $ 2 $ and $ 3 $. $ -5 $ is a quadratic residue modulo $ 2 $ and $ 3 $, therefore both primes are split. However, the norm form for this ring is $ a^2 + 5b^2 $, and this can never equal $ \pm 2 $ or $ \pm 3 $, hence $ \mathbf Z[\sqrt{-5}] $ is not a principal ideal domain. We can compute its ideal class group: $ (2) = \mathfrak p^2 $ and $ (3) = \mathfrak q \mathfrak q' $, and it is easy to see that $ (1 + \sqrt{-5}) = \mathfrak p \mathfrak q $, so $ \mathfrak p \sim \mathfrak q $ which has order $ 2 $. This implies that the ideal class group is cyclic of order $ 2 $.
In fact, 2 is always a ramified prime in $ \mathbf Z[\sqrt{-n}] $ for $ n \geq 3 $ square-free (and not 3 modulo 4 - we covered this case above), however the norm form is $ a^2 + n b^2 $ which can never equal $ \pm 2 $. We conclude that none of these number rings are principal ideal domains.
For $ \mathbf Z[\sqrt{7}] $ the Minkowski bound is $ M_K \approx 10.5 $, so it suffices to look at the primes $ 2, 3, 5, 7 $. By our criterion, the primes $ 2, 3, 7 $ are split. Moreover, we have $ N(3 + \sqrt{7}) = 2 $, $ N(2 + \sqrt{7}) = -3 $ and $ N(\sqrt{7}) = -7 $. We thus conclude that $ \mathbf Z[\sqrt{7}] $ is a principal ideal domain.