The inspiration for integration by parts comes from the Product Rule when differentiating. Referring to f(x) and g(x) as f and g, respectively, for less cluttering:
d/dx(f * g) = f * g´ + g * f´
Integrating both sides we get
f * g = ∫(f * g´)dx + ∫(g * f´)dx, therefore
f * g - ∫(f * g´)dx = ∫(g * f´)dx <~~ equation (1) ~~
Since we haven't defined the functions f and g explicitly, there's some symmetry in this expression. So all that matters is that the integral we have to evaluate will change from ∫(g * f´) to ∫(f * g´). With this in mind, we can see that in the given example the two functions are e^x and cos(x).
In this particular example, it just so happens that it doesn't matter which function we choose for g and f, we can reach the answer in a similar way (good exercise to try out), so we'll choose f = cos(x) and g = e^x. Then, for the right hand side on expression (1) above,
∫(g * f´)dx = ∫(e^x) * (cos(x)) dx,
and now can calculate the terms on the left hand side,
g * f = e^x * sin(x), and
∫(f * g´)dx = ∫(e^x) * (sin(x)) dx,
=> e^x * sin(x) - ∫(e^x) * (sin(x)) dx = ∫(e^x) * (cos(x)) dx <~~ equation (2) ~~
From here, we do integration by parts again for the integral on the left hand side, ∫(e^x) * (sin(x)) dx. If we choose e^x as f and sin(x) as g using equation (1),
e^x * sin(x) - e^x * sin(x) + ∫(e^x) * (cos(x)) dx = ∫(e^x) * (cos(x)) dx,
=> 0 = 0.
So instead we should choose e^x to differentiate and sin(x) to integrate, namely e^x as g and sin(x) as f in equation (1), and
-e^(x) * cos(x) + ∫(e^x) * (cos(x)) dx = ∫(e^x) * (sin(x)) dx.
Then we substitute ∫(e^x) * (sin(x)) dx in equation (2) for the left hand side above, and we get
e^x * sin(x) + e^(x) * cos(x) - ∫(e^x) * (cos(x)) dx = ∫(e^x) * (cos(x)) dx,
e^x * (sin(x) + cos(x)) = 2 * ∫(e^x) * (cos(x)) dx,
e^x * (sin(x) + cos(x)) / 2 = ∫(e^x) * (cos(x)) dx,
where the value in the left hand side equals ∫(e^x) * (cos(x)) dx. So we calculated the expression for the original integral using integration by parts twice. Hope that helps!