You are right...! There should be a $\cos\theta$ factor. If you choose a path $\mathcal{P}$ from $\infty$ to $r$, then along this path, you have to evaluate
$$
\mathbf{F} \cdot d\mathbf{r} = |\mathbf{F}| |d\mathbf{r}| \cos\theta
$$
The angle $\theta$ will also depend on where you are on the path compared to the local direction of the force $\mathbf{F}$. True.
But when you learnt that in electrostatics one can define an electric potential, you must also have learnt that this potential is related to the electric field via
$$\mathbf{E} = -\nabla U$$
The reason you can write this way is because the electric field is irrotational, i.e.,
$$\nabla \times \mathbf{E}=0$$
If this makes sense to you, then you must also know that via Stokes theorem the line-integral of $\mathbf{E}$ between two points is independent of the path between those points. Since $\mathbf{F} = q\mathbf{E}$, we have
$$\int_A^B \mathbf{F} \cdot d\mathbf{r}$$
is independent of the path chosen for going from $A$ to $B$. So you can just choose a path such that this $\cos\theta$ factor is a constant. For a point charge, $\mathbf{E}$ will be radially directed from the charge. Choose the path along a radial direction from $\infty$ to $r$. Then $\cos\theta = \pm 1$ depending on the sign of the charge and comes out of the integral.
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.
Best Answer
When you calculate work, you do so along a given path. Here, that path has tangent vector $d\mathbf s$. This is a vector with direction; the minus sign will ultimately come from choosing the path's orientation--inward or outward.
Edit: Aha, I think I've found the unintuitive part. The key is in the use of the coordinate $r$ to parameterize the path, in that $r$ is larger at the start of the path and smaller at the end. This runs counter to what you would usually do when parameterizing such a path with an arbitrary parameter.
Let $\mathbf s_0$ and $\mathbf s_1$ be the starting and ending points of a path $\mathbf s(\lambda) = \mathbf s_0 + (\mathbf s_1 -\mathbf s_0)\lambda$. The work integral is then
$$W = \int_{\mathbf s_0}^{\mathbf s_1} \mathbf F(\mathbf s) \cdot d\mathbf s= \int_0^1 \mathbf F(\mathbf s(\lambda)) \cdot \frac{d\mathbf s}{d\lambda} \, d\lambda = \int_0^1 \mathbf F (\mathbf s(\lambda))\cdot (\mathbf s_1 - \mathbf s_0) \, d\lambda$$
For two finite points, the basic approach is sound, but it breaks down when you have a point at infinity involved. This is the reason that the problem of assembling a configuration is usually attacked with a different basic parameterization.
Instead, set $\mathbf s(\lambda) = \lambda \hat{\mathbf a}$ for some unit vector $\hat{\mathbf a}$ and set the bounds of the integral as being from $[\infty, R)$. This is the important point: even though the path is being traversed coming in from infinity, the parameterization means that $d\mathbf s/d\lambda = + \hat{\mathbf a}$, not minus as I originally thought. The path's still oriented outward; we're just traversing it backwards.
Here's how that integral looks:
$$W = \int_{\infty}^R \mathbf F(\lambda \hat{\mathbf a}) \cdot \hat{\mathbf a} \, d\lambda$$
Of course, we know the expression for the electric force:
$$\mathbf F(\mathbf r) = k\frac{qq_0 \mathbf r}{|r|^3}$$
Plug in $\mathbf r = \lambda \hat{\mathbf a}$ to get
$$\mathbf F(\lambda \hat{\mathbf a}) = k \frac{qq_0 \lambda \hat{\mathbf a}}{\lambda^3} = k \frac{q q_0 \hat{\mathbf a}}{\lambda^2}$$
We find that the integrand is then
$$W = \int_{\infty}^R k \frac{q q_0}{\lambda^2} \hat{\mathbf a} \cdot \hat{\mathbf a} \, d\lambda = \int_\infty^R k \frac{q q_0}{\lambda^2} \, d\lambda = - k \frac{q q_0}{R} < 0$$
The work is negative, so the change in potential energy $\Delta U = - W$ is positive as required.
So where is the problem then? As we've seen, there actually shouldn't be an extra negative sign coming in on line 4 (as posted in the OP's question). This is somewhat obscured because an explicit parameterization of the path is never written down in the first place--usually, you don't have to, but this problem is tricky enough that it helps immensely.