This question has several good answers already, but here's my attempt at an explanation that avoids differential forms notation, and emphasizes that we're using the multivariable chain rule in reverse.
Consider the ODE \begin{equation} p(x,y(x)) + q(x,y(x)) y'(x) = 0. \end{equation} (Assume that $p$ and $q$ are continuously differentiable in a rectangle $R = (a,b) \times (c,d)$.)
If we can find a function $F$ such that $\frac{\partial F}{\partial x} = p$ and $\frac{\partial F}{\partial y} = q$, then (by the multivariable chain rule) \begin{align} \frac{d}{dx} \, F(x,y(x)) &= \frac{\partial F(x,y(x))}{\partial x} + \frac{\partial F(x,y(x))}{\partial y} y'(x) \\ &= p(x,y(x)) + q(x,y(x)) y'(x) \end{align} and our ODE can be written as \begin{equation} \frac{d}{dx} \, F(x,y(x)) = 0. \end{equation} We can now take anti-derivatives of both sides to obtain \begin{equation} F(x,y(x)) = k \end{equation} (for some constant $k$) and solve for $y(x)$.
When is it possible to find such a function $F$? One observation is that if such an $F$ exists, then $\frac{\partial^2 F}{\partial y \partial x} = \frac{\partial p}{\partial y}$, and $\frac{\partial^2 F}{\partial x \partial y} = \frac{\partial q}{\partial x}$. By equality of mixed partials, we see that \begin{equation} \frac{\partial p}{\partial y} = \frac{\partial q}{\partial x}. \end{equation} This is a necessary condition for such an $F$ to exist. When this condition is satisfied, our ODE is said to be "exact".
It turns out that this condition is also sufficient. To see this, assume that $\frac{\partial p}{\partial y} = \frac{\partial q}{\partial x}$, and let's find an $F$ that works.
Let $P$ be an anti-derivative of $p$ with respect to $x$. From the requirement that $\frac{\partial F}{\partial x} = p$, we get \begin{equation} F(x,y) = P(x,y) + C(y) \end{equation} for some function $C(y)$. ($y$ is now being used as a "dummy variable".)
The requirement that $\frac{\partial F}{\partial y} = q$ gives us \begin{align} \frac{\partial F(x,y)}{\partial y} &= \frac{\partial P(x,y)}{\partial y} + C'(y) \\ &= q(x,y) \end{align} which implies that \begin{equation} C'(y) = q(x,y) - \frac{\partial P(x,y)}{\partial y}. \end{equation} This equation might seem impossible at first, because the right hand side apparently depends on $x$, whereas the left hand side depends only on $y$. However, if you take the derivative of the right hand side with respect to $x$, and use our assumption that $\frac{\partial p}{\partial y} = \frac{\partial q}{\partial x}$, you get $0$. This shows that the right hand side actually does not depend on $x$ after all.
In summary, to find $F$, let $P$ be an anti-derivative of $p$ with respect to $x$, pick $C(y)$ such that $C'(y) = q(x,y) - \frac{\partial P(x,y)}{\partial y}$, and let $F(x,y) = P(x,y) + C(y)$.
Alternatively, we could let $Q$ be an anti-derivative of $q$ with respect to $y$, and find an $F$ of the form $F(x,y) = Q(x,y) + C(x)$.
In the problem in this question, we have \begin{equation} p(x,y) = 2xy^2 + \cos(x) \end{equation} and \begin{equation} q(x,y) = 2x^2y + \sin(y). \end{equation}
Note that $\frac{\partial p(x,y)}{\partial y} = \frac{\partial q(x,y)}{\partial x} = 4xy$, so the ODE is exact.
$P(x,y) = x^2 y^2 + \sin(x)$ is an anti-derivative of $p$ with respect to $x$. We want to find $C(y)$ such that \begin{align} C'(y) &= q(x,y) - \frac{\partial P(x,y)}{\partial y} \\ &= 2x^2 y + \sin(y) - 2x^2 y \\ &= \sin(y). \end{align} So let $C(y) = -\cos(y)$, and let \begin{align} F(x,y) &= P(x,y) + C(y) \\ &= x^2 y^2 + \sin(x) - \cos(y). \end{align} Then $\frac{\partial F}{\partial x} = p$ and $\frac{\partial F}{\partial y} = q$. The solution to our ODE is $F(x,y(x)) = k$, or in other words \begin{equation} x^2 \, y(x)^2 + \sin(x) - \cos(y(x)) = k. \end{equation}