Magnetic fields are generated by moving charged particles, and exert forces on moving charged particles.
The magnetic field generated by a moving charged particle can be calculated using Biot-Savart's law. Since a current is a steady stream of charged particles, the magnetic field created by a current-carrying wire can be calculated integrating over the length of the wire. The exact formula in classical electromagnetism is:
$$\mathbf{B} =\frac{\mu_0 q \mathbf{v}}{4\pi} \times \frac{\mathbf{r}}{r^2}$$
You want to pay close attention to the two vector quantities, v and r, the velocity of the charged particle and the position in space from it, and to the fact that they are combined with a cross product, $\times$. The resulting magnetic field, B, is therefore perpendicular to both. So a single electron moving in a straight line basically generates a magnetic field that goes around in circles around the path it is moving along. This picture may help.
Then you have the force acting on a moving particle when there is a magnetic field. This is known as Lorentz's Force, and the equation describing it is:
$$\mathbf{F} = q\ \mathbf{v} \times \mathbf{B}$$
You again have a cross product, this time involving the magnetic field, and again v, although this is a different v than before, not the velocity of the moving charged particle generating the field, but of the moving charged particle the field is acting on. Because of the cross product, a particle will not have any force acting upon it if it is moving parallel to the magnetic field. That's exactly why there is no force on the closed loop in your picture: because it lays along the direction of the magnetic field.
You could combine both equations into a single one, and so the force on particle 2 by particle 1, $\mathbf{F_{21}}$, if $\mathbf{r_{21}}$ is the position vector of particle 2 from particle 1, would then be
$$\mathbf{F_{21}} =\frac{\mu_0 q_1 q_2}{4\pi\ r^2}\mathbf{v_2} \times \mathbf{v_1} \times \mathbf{r_{21}}$$