I think you just forgot that the $\int_A^B F\,dl$ is not a scalar expression. Rather it should be written in a form $\int_A^B \vec{F}\cdot d\vec{l}$. Then it comes to the sign of the scalar product:
$$\vec{F}\cdot d\vec{l}=F\,dl\,\cos\theta$$
where the angle $\theta$ is taken between the vector $\vec{F}$ and the direction of the tangent to the integration path from $A$ to $B$. Then, in your first example,
$W_{A \to B}=\int_{r_A}^{r_B} F(r) dr = \int_{r_A}^{r_B} \left(-\frac{GMm}{r^2} \right)dr$
the path could go with any slope, but the gravity is always directed downwards, along the $r$ axis. That means, we can always take $(\pi-\theta)$ as the angle between the vector $d\vec{l}$ and the $r$ axis, that is
$$dl\,\cos(\pi-\theta)=dr$$
but $\cos(\pi-\theta)=-\cos\theta$ and thus we have
$$\vec{F}\cdot d\vec{l}=-F\,dr=-\frac{GMm}{r^2}dr$$
For your second example:
...we also should change the sign, because the gravitational force is always a force of attraction.
what the authors actually mean is that: the Coulomb's and Newton's forces have exactly the same expressions, but the sign conventions for them are different. The Newton's force is defined that if all the quantities ($M$, $m$ and $r$) are positive, then the vector of the force is directed towards the other body. But for the Coulomb's force, if all the quantities ($q_1$, $q_2$ and $r$) are positive, then the vector of the force is directed away from the other charge. That becomes manifest if we take the vector expressions for these forces:
$$\vec{F}_N=-\frac{GMm\,\vec{r}}{r^3}\qquad\vec{F}_C=\frac{q_1q_2\,\vec{r}}{4\pi\epsilon_0\,r^3}$$
Now the different signs are clearly seen.
"...from point $A$ to point $B$..." - ...as I understand it - the work that I must do is always $U_B-U_A$. However the work that the force that is being created by the field do_es_ is always $U_A-U_B$, am I correct?
Yes this is correct.
The mnemonic rule is very simple: $U$ is like the height of the slope. When you go up, $U_B>U_A$, and it is you who does the work. But when you go down, $U_A>U_B$, and it is the field force who does the work.
There's always confusion with this topic when it's not well explained. It's all inside "work-energy theorem", which says
$$\Delta E_k = W$$
But we'll make a distinction here: work done by conservative forces and work done by non conservative forces:
$$ \Delta E_k = W_C + W_{NC} $$
And now, we just call "minus potential energy" to the work done by conservative ones
$$W_{C}:= -\Delta E_p$$
We do this for convenience. We can do it, because a conservative force is such taht can be written as a substraction of a certain function $B$ like this:
$$W_C=B(\vec{x_f})-B(\vec{x_0}) $$
We just decide to define $E_p=-B$, so $W_{C}=-\Delta E_p$. We include that minus sign so that we can take it to the LHS:
$$ \Delta E_k = W_C + W_{NC} $$
$$ \Delta E_k = -\Delta E_p + W_{NC} $$
$$ \Delta E_k + \Delta E_p = W_{NC} $$
$$ \Delta E_m = W_{NC} $$
So the increment in mechanical energy is always equal to the work done by non-conservative forces. If there are no non-conservative forces, then $\Delta E_m=0$ and energy is conserved (that's why we call them like that.
(read it slowly and understand it well)
So, having this in mind, I think your confusion arises because of that famous "artificial" negative sign.
There are many formulas, and it's typicall to have a mess. It's all about surnames: $\Delta E_k = W_{Total}$, but $\Delta E_m=W_{NC}$. The subindices are the key.
The force of engines is non-conservative. Hence, their work contributes to total mechanical energy.
Gravity is conservative, so we can work with its potential energy.
If there is no increase of kinetic energy, that means
$0 + \Delta E_p = W_{NC}$
So engines are only increasing potential energy. But that means
$$-W_C = W_{NC}$$
Of course, if there's no gain in KE, no acceleration, there's equilibrium. The work of the engines is compensating the work of gravity.
- Negative work is always positive $\Delta E_p$, by definition.
- More altitude means more $E_p$, you are right. But here energy is not conserved (engines). Normally, increasing height would decrease $E_k$, but we're adding work so taht $E_k$ stays constant.
- $\Delta E_k=0$ implies $W_{Total}=0$. That means gravity is making negative work, and engines are doing positive work (equilibrium). The thing is that potential energy variation is minus gravity's work.
Best Answer
It would help me, but even more importantly you, if you defined what your symbols are supposed to mean. You did that with $U_i$ and $U_j$. So let's be precise together, as an exercise:
Let's explicitly state that we consider a point mass $m$ in a gravity field caused by a much larger mass $M_E$ at the origin of the coordinate system, so that we can assume that $M_E$ does not move. Let's further specify that $U_i$ is the potential energy of $m$ at the initial position, and that $U_f$ is the potential energy of $m$ at the final position.
Potential energies are not absolutely measurable; one can only measure differences in potential energies. So, the potential energy is only fixed up to an additive constant. Now, one may (and should) ask how this constant is chosen in your problem. From the form of the potential energy you used, $$ U\left(\vec{r}\right) = -G \frac{M_E m}{r} $$ I conclude that your choice is such that the potential energy is $0$ at infinite distances to the origin: $U\left(\infty\right) = 0$
For the sake of argument, let's further say that in the inital position $R_E$ is closer to the origin than the final position $R_E+h$, so that your $h$ is greater than $0$. Then, $$ U_i = U\left(R_E\right) = - G \frac{M_E m}{R_E} < - G \frac{M_E m}{R_E + h} = U\left(R_E+h\right) = U_f $$ That is, the mass $m$ has lower potential energy closer to the origin.
This means that moving $m$ from the initial position to the final position requires the input of work by some external source. Now, there are two different conventions as to how to label this work; some say that the work that acts on $m$ to move it is called $W$. Others do the opposite, and use $W$ to denote the work performed by $m$ as it moves. These two conventions differ exactly by a factor of $-1$.
So, when you write $W = U_i - U_f$, you choose to say that $W$ is the work done by $m$ as it moves. You can convince yourself of this fact by considering the example discussed above, where $U_i < U_f$. Then clearly, $W<0$, which makes sense, since $m$ does not actually perform work, but work is performed on it in order to move it.
However, when you say
you could have said more precisely
But in the case of $W^\prime$, the choice of convention for the sign of work is opposite to the choice for $W$ you used earlier, so that $W=-W^\prime$, and the apparent contradiction is now resolved. If it isn't, just let me know, I'll try to reformulate.