If $(A,\mathfrak p)$ and $(B,\mathfrak q)$ are local rings with only one prime ideal, then $R=A\times B$ has this property: a prime ideal must contain either $(1,0)$ or $(0,1)$ since their product is $0$, and then it is easy to see the prime must be either $A\times\mathfrak q$ or $\mathfrak p\times B$, respectively. (More generally, if $R=A\times B$ for any two rings $A$ and $B$, the primes in $R$ are exactly the sets of the form $A\times \mathfrak q$ or $\mathfrak p\times B$ where $\mathfrak q$ is a prime of $B$ or $\mathfrak p$ is a prime of $A$.)
Conversely, every example has this form. Indeed, if $R$ is not local and has exactly two prime ideals, both prime ideals must be maximal. It follows that $\operatorname{Spec}(R)$ is a discrete space with two points $\mathfrak p$ and $\mathfrak q$. From the fact that the structure sheaf on $\operatorname{Spec}(R)$ is a sheaf it follows that the canonical map $R\to R_{\mathfrak p}\times R_{\mathfrak q}$ is an isomorphism (since $R_{\mathfrak p}$ is exactly the value of the structure sheaf on the open set $\{\mathfrak p\}$ and $R_{\mathfrak q}$ is exactly the value of the structure sheaf on the open set $\{\mathfrak q\}$).