According to NASA, the apparent retrograde motion of the planets is caused by the difference in the orbital speeds of the planets. For example, since Mars takes just about twice as long to make one trip around the sun as the Earth, when Earth passes Mars in orbit, the illusion of retrograde motion occurs.
NASA explains the illusion by likening the planets to race cars on an oval track. As Earth comes from behind to overtake Mars, since Earth takes about half the time to complete a lap, every two years it looks as though Mars is moving up and down. As Earth moves further along its curved orbit, the illusion disappears and Mars again appears to be moving in a straight line. In addition, the orbits of Earth and Mars don't quite lie in the same plane and are slightly tilted with respect to each other. This causes another illusion in which Mars moves in a zigzag pattern, as opposed to a backward loop. The illusion occurs not only with Mars but all the other planets that orbit further from the sun. According to LaSalle University, Copernicus is credited with the first models that represent our current understanding of retrograde motion, and the model was further refined by Kepler.