The magnets provide the magnetic field, as well as contacts that connect the coil of wire to the battery. When both sides are connected to the wire, it generates an electromagnetic field around the coil. Because of the directions the magnets are facing (and the polarity of the magnetic field created by the current through the coil), the magnetic fields repel each other, which pushes the battery through. When this happens, though, it simply energizes another part of the coil and the process repeats until one side of the battery no longer connects to the wire.