Horseshoe orbits come from the most counterintuitive behavior of orbits: if you thrust forward, you go slower. And if you thrust backward, you go faster. (The reason is that thrusting forward puts you in a higher orbit.)
In a horseshoe orbit, when the small body approaches the medium body "from behind" (that is, the small body is moving faster than the medium body), the medium body tugs the small body forward. That is an effective forward thrust for the small body, which rises into a higher orbit and slows down as a result. That means the small body starts to fall behind, losing ground relative to the medium body.
After it loses enough ground, it approaches the medium body from the front (or, if you prefer, the medium body catches up to it from behind). Then the medium body's gravity tugs it backward, dropping it into a lower and faster orbit, and the cycle repeats.
The most exceptional example of this is two of Saturn's moons, Janus and Epimetheus, which share an orbit and periodically trade places in it as a result of these dynamics.
In a horseshoe orbit, when the small body approaches the medium body "from behind" (that is, the small body is moving faster than the medium body), the medium body tugs the small body forward. That is an effective forward thrust for the small body, which rises into a higher orbit and slows down as a result. That means the small body starts to fall behind, losing ground relative to the medium body.
After it loses enough ground, it approaches the medium body from the front (or, if you prefer, the medium body catches up to it from behind). Then the medium body's gravity tugs it backward, dropping it into a lower and faster orbit, and the cycle repeats.
The most exceptional example of this is two of Saturn's moons, Janus and Epimetheus, which share an orbit and periodically trade places in it as a result of these dynamics.