All of the planets in the Solar System orbit the Sun in the same direction as the Sun’s rotation, i.e. anticlockwise, looking from above the Sun’s north pole. These are known as ‘prograde’ orbits (literally ‘moving forwards’). Their direction is a consequence of the initial rotation of the cloud of gas and dust from which the Sun and planets formed.
A planet with a ‘retrograde’ orbit, i.e. moving in the opposite direction to its home star’s rotation, would require a massive change in energy to reverse its initial prograde orbit, and this is why planets with retrograde orbits are rare. (Retrograde orbits are more common with comets and asteroids, because they’re smaller and easier to move out of their original orbits.)
But retrograde planets do exist – two examples are the exoplanets Kepler-2b and WASP-17b. The process responsible may be what’s known as the ‘Kozai mechanism’, in which the gravitational effect of a distant third body can cause a perturbation in a planet’s orbit that slowly moves the planet into a tilted and elongated orbit. This tilt eventually becomes so extreme that the orbit is flipped over.
Another possible mechanism for reversing a planet’s orbit is the gravitational effect of a close encounter or collision with a large body. However, most of these interactions would likely destroy the planet completely, or at least expel it from the star system. It’s also possible that a star’s disc of gas and dust, from which its planets form, can itself be flipped over if the disc strays too close to another clump of matter. Any planets formed in that disc would then have retrograde orbits.