I) OP is given a problem of the form
$$\tag{1} \dot{q}~=~f(q,p), \qquad \dot{p}~=~g(q,p), $$
where $f$ and $g$ are two given smooth functions. OP is asked to derive the integrability condition for the eqs. (1) to be Hamilton's eqs.
$$\tag{2} \dot{q}~=~\frac{\partial H}{\partial p}, \qquad
\dot{p}~=~-\frac{\partial H}{\partial q}.$$
OP correctly deduces that
$$\tag{3} f~=~\frac{\partial H}{\partial p}, \qquad
g~=~-\frac{\partial H}{\partial q},$$
and that the integrability condition is the Maxwell-type relation
$$\tag{4} \frac{\partial f}{\partial q}+\frac{\partial g}{\partial p}~=~0.$$
Michael Brown in a comment then explains that if
$$\tag{5} F(q,p)~=~\int^{p}\!dp^{\prime}~f(q,p^{\prime})$$
is some antiderivative/primitive integral/indefinite integral of the given $f$ function, so that
$$\tag{6} \frac{\partial F}{\partial p}~=~f,$$
then eqs. (3a) and (6) imply that
$$\tag{7} \frac{\partial (H-F)}{\partial p}~=~0.$$
In other words, the difference $H-F$ cannot depend on the $p$ variable. It could be an arbitrary function $G(q)$ of the $q$ variable only. So the Hamiltonian is of the form
$$\tag{8} H(q,p)~=~F(q,p)+G(q). $$
Finally, one gets restrictions on the $G$ function by plugging eq. (8) into eq. (3b). This should answer OP's question (v4), and we are in principle done.
II) However, we cannot resist making the following general point about the existence of a Hamiltonian formulation. OP's title question is a bit academic if one a priori insists that the variables $q$ and $p$ should play the role of canonical variables. Why would one insist on that? The goal is, after all, just to get a Hamiltonian formulation, whatever it takes. So a more realistic question is the following more general problem.
Suppose that one is given a two-dimensional first-order problem
$$\tag{9} \dot{x}~=~f(x,y), \qquad \dot{y}~=~g(x,y), $$
where $f$ and $g$ are two given smooth functions. Is eq. (9) a Hamiltonian system
$$\tag{10} \dot{x}~=~\{x,H\}, \qquad \dot{y}~=~\{y,H\}, $$
with a symplectic structure $\{\cdot,\cdot\}$ and Hamiltonian $H(x,y)$?
The answer is, perhaps surprisingly: Yes, always, at least locally.
Proof: In two dimensions, a Poisson bracket is completely specified by the fundamental Poisson bracket relations
$$\tag{11} \{x,y\} ~=~B(x,y)~=~-\{y,x\}, \qquad \{x,x\}~=~0~=~\{y,y\}, $$
where $B$ is some function that doesn't take the value zero. [Exercise: Check that eqs. (11) automatically satisfy the Jacobi identity.] The Hamilton's eqs. (10) become
$$\tag{12} \dot{x}~=~B\frac{\partial H}{\partial y}, \qquad \dot{y}~=~-B\frac{\partial H}{\partial x}. $$
Next consider the one-form
$$\tag{13} \eta ~:=~ f{\rm d}y -g{\rm d}x, $$
which is possibly an inexact differential. However, it is known from the theory of PDE's, that there locally exists an integrating factor $\frac{1}{B}$, so that the one-form
$$\tag{14} \frac{1}{B}\eta~=~{\rm d}H $$
is locally an exact differential given by some function $H$. It is straightforward to check that one can use $B$ as the Poisson structure (11) and $H$ as the Hamiltonian.
Remark. The existence of a pair of canonical variables $q(x,y)$ and $p(x,y)$, with $\{q,p\}=1$, are, in turn, guaranteed locally by Darboux' Theorem.