The force field indeed must be conservative, and this is the criterion for your ability to express force in terms of potential energy. To test this let a force field (in 2D for simplicity) be given by
$$F=f_i+g_j$$
where $f$ and $g$ are functions of $x$, $y$
then the partial differential of $f$ with respect to $y$ must be equal to the partial differential of $g$ with respect to $x$. This is a general result in mathematics, to test for conservative vector fields.
In the case of a simple Newtonian gravitational field, $$f=GmMx/(x^2+y^2)^{3/2}$$
Integrating with respect to $x$ yields $-GmM/(x^2+y^2)^{1/2}+C(y)$, where $C(y)$ is the constant of integration. Differentiating this result with respect to y yields
$GmMy/(x^2+y^2)^{3/2}+dC(y)/dy$.
Equating this to $g$ let us compute $dC(y)/dy=0$ and therefore $C(y)=C$, where $C$ is a constant independent of $x$ or $y$.
Hence the potential energy is given by
$$Ep=-GmM/(x^2+y^2)^{1/2}+C$$