The stitches in a mechanical machine are made by a tracer that follows a cam, that moves the needle. On some mechanical machines, you can put it in the "stretch" mode, which makes the feed dogs move forward and backward.
In a computerized machine, there are stepping motors that make the stitches. One moves the needle and the other moves the feed dogs, and together, you get some amazing decorative stitches.