This deceptively simple mechanism is controlled by a complex series of gears, cams, belts and shafts that are powered by a single motor.
For a more detailed explanation of how this all works, complete with animated gifs, check out this post. Every time the needle moves up and down through the fabric, the thread from the needle above the fabric is caught by the thread from the bobbin beneath the fabric, creating a lock stitch that secures the layers of fabric together. When you sew, you insert your fabric between the needle portion and the bobbin portion of the machine. In short, an arm that holds a needle with one thread is positioned over a base plate that holds a bobbin with another thread. All sewing machines function on the same basic system.