I asked myself the same question as that by OP. I arrived at the fact that to find $f(x)$ one should solve the differential equation given above by mfl above. But the solution of the differential equation requires to compute first the integral of $g(x)e^x$, so a circular problem.
What OP is asking is how to solve the differential equation without doing integral calculus and when the solution $f(x)$ is found then we claim that the integral of $g(x)e^x$ is nothing but $f(x)e^x+C$.
To arrive at a satisfactory answer, do not ask yourself how to find a way to find $f(x)$ for ANY given $g(x)$. But ask how to find $f(x)$ for some classes of $g(x)$, as polynomials as so on. Classes we do find in textbooks exercises.
Actually, I did find a way for the following classes of $g(x)$ we find in textbooks.
Polynomials (of any degree),
sin x, cos x and any linear combinaison of sin x and cos x, next,
$x \sin x$ and $x \cos x$ and any linear combinaison of them, next
$x^{2}\sin x$ and $x^2\cos x$ and any linear combinaison of them, next
Some partial fractions when the degree of the denominator is 2.
I also have tested the way to find $f(x)$ for each class cited above for most concrete examples in some textbooks (old and new). I do not believe that it is always better than usual integration by parts.
The same technique works for case $f'(x)+g'(x)f(x)$. Recall that $$\int (f'(x)+g'(x)f(x))e^{g(x)} dx = f(x)e^{g(x)}+C.$$
What i did discover of great value (at least for me) is that the $g(x)$ of ANY concrete textbook exercise is of the form $f(x)+f'(x)$ (or the form $f'(x)+g'(x)f(x)$) and the $f(x)$ can be found almost easily even for the following monster-looking example
Computing an awful integral
i did find the solution easily without trial-error method but by following a straight path!
When time permitted i will give some examples of finding $f(x)$ for $g(x)$ belonging to some of the cited classes.
EDIT: Here is a first example dealing with polynomial case
Evaluate the integral $\int \left( 2x^{5}+x^{3}+x\right) e^{x^{4}+x^{2}}dx$ It is of the form
\begin{equation*}
\int \left( 2x^{5}+x^{3}+x\right) e^{x^{4}+x^{2}}dx=\int h(x)e^{g(x)}dx.
\end{equation*}
This form recalls the well-known formula
\begin{equation*}
\int \left( f^{\prime }(x)+g^{\prime }(x)f(x)\right)
e^{g(x)}dx=f(x)e^{g(x)}+C.
\end{equation*}
So we are done if we find a function $f(x)$ such that
\begin{equation*}
h(x)=f^{\prime }(x)+g^{\prime }(x)f(x).
\end{equation*}
In what follows, I will show that $f(x)=\frac{1}{2}x^{2},$ and therefore
\begin{equation*}
\int \left( 2x^{5}+x^{3}+x\right) e^{x^{4}+x^{2}}dx=\left( \frac{1}{2}
x^{2}\right) e^{x^{4}+x^{2}}+C.
\end{equation*}
$\color{red}{\bf Problem:}$ We want to write $\left( 2x^{5}+x^{3}+x\right) $ as $
f^{\prime }(x)+g^{\prime }(x)f(x)$ where $g(x)=x^{4}+x^{2},$ $g^{\prime
}(x)=4x^{3}+2x$ and $f(x)$ is to be determined.
First, it is easy to see that
\begin{equation*}
\left( 2x^{5}+x^{3}+x\right) =x+(2x^{3}+x)(x^{2})=x+(4x^{3}+2x)(\frac{x^{2}}{
2})=x+g^{\prime }(x)(\frac{x^{2}}{2}).
\end{equation*}
If we put $f(x)=(\frac{x^{2}}{2}),$ then
\begin{equation*}
f^{\prime }(x)+g^{\prime }(x)f(x)=\left( x\right) +(4x^{3}+2x)\left( \frac{
x^{2}}{2}\right) =x+2x^{5}+x^{3}=\left( 2x^{5}+x^{3}+x\right) ,
\end{equation*}
we are done! Then, it suffices to take
\begin{equation*}
f(x)=(\frac{x^{2}}{2}).\ \ \
\color{red}
\blacksquare
\end{equation*}
EDIT: the following is of interest
$f(x) - f'(x) = x^3 + 3x^2 + 3x +1; f(9) =?$
EDIT: this one is of interest too
Proof for formula $\int e^{g(x)}[f'(x) + g'(x)f(x)] dx = f(x) e^{g(x)}+C$
EDIT: I have provided an example here
Computing $\int (1 - \frac{3}{x^4})\exp(-\frac{x^{2}}{2}) dx$
EDIt another example here
Evaluating integral $\int\frac{e^{\cos x}(x\sin^3x+\cos x)}{\sin^2x}dx $
and The monster integral here
Compute $\int_0^{\pi/4}\dfrac{(1-x^2)\ln(1+x^2)+(1+x^2)-(1-x^2)\ln(1-x^2)}{(1-x^4)(1+x^2)} x\exp\dfrac{x^2-1}{x^2+1} dx$