First, let me state what I understand to be the first-order rendition of Peano's 5th axiom: the axiom of induction. For all natural numbers, for any relation/property/predicate $R$...
$$(R(0) \land \forall x[R(x) \rightarrow R(S(x))]) \rightarrow \forall x(R(x))$$
(first question: is this a correct formalization of this axiom or not?)
How does this axiom prevent elements of natural numbers that have '$S$-loops' that is:
\begin{align} S(a) = b && \text{and} && S(b) = a \end{align} (edited for clarity)
I think your formalization is correct.
The axiom of induction doesn't prevent by itself S loops. Consider, a two elements set $\{0,1\}$ with $S(0) = 1$ and $S(1) = 0$
To prevent S loops you need axioms
4."Two numbers of which the successors are equal are themselves equal."
$$ \forall a,b \; . \; S(a) = S(b) \Rightarrow a = b $$
3."$0$ is not a succesors"
$$ \forall a\;.\;S(a)\neq0 $$
Informally, if you have a loop from combination of (5) and (4) it follows that loop need to involve all predecessors of a looped element. And with this (3) provides a contradiction, as by (5) every element has $0$ as a predecessor.
The more formal proof can look like this.
Consider the predicate $$NL(x) = \text{$x$ is not an element of any loop.} $$ By definition of the loop, if $a$ is in the loop, there must be $b$ in the loop, such that $a = S(b)$. So, $NL(0)$ holds by axiom (3). Now consider an element $a$ such that $NL(a)$ holds. If $S(a)$ is an element of the loop, then by the axiom (4) the element $a$ is also in the loop. This is a Contradiction! Thus, $NL(a) \Rightarrow NL(S(a))$ holds for any $a$. Now can apply axiom of induction to see that there is no element $a$, which can be looped.
So there are no loops in Natural numbers defined by Peano axioms. Note, however, that you need every axiom to prove it.