For most motor projects, you will want more power than you can get directly from the pins of the microprocessor. The good news is that this is fairly easy.
We can use a transistor to turn the power on and off to the motor, controlled by the pin from the microprocessor.
In the photo above, the motor is getting its power directly from the same place the computer is (in this case that is a pair of C batteries in a battery holder under the solderless breadboard). The solderless breadboard has a transistor plugged into it. The transistor is acting as a switch. When the center pin of the transistor is brought up to 3 volts by the pin on the microprocessor, it turns the transistor on. The current then can flow through it, connecting the motor to the batteries. When the microprocessor pin is off (at zero volts, the ground level), then the transistor turns off, and the motor stops.
The transistor can be any NPN transistor, such as the 2N2222A transistor shown in the photo above.
We can use Pulse Width Modulation to control the speed of the motor, but this circuit cannot change the direction of the motor.
Only a tiny amount of current is used from the computer pin, so the processor does not heat up. The transistor is controlling all of the current going through the motor, and it can handle a whopping 800 milliamperes. If you need more than that, there are power transistors that can handle many watts of power.
But we can get more power even from the little 2n2222A. We can raise the voltage up to as much as 60 volts. With 60 volts and 800 milliamperes, we can control 48 watts, which is more than most small DC motors can handle.
Here's how we do it. We'll use a nine volt battery in this example. The two power supplies (the C cells for the computer and the nine volt for the motor) will have their grounds connected together. The black wire from the nine volt battery is plugged into the hole in the solderless breadboard that the emitter of the transistor is plugged into. The blue wire from the ground on the computer is also plugged into a hole that connects to the emitter.
The microprocessor pin is connected to the base, like it was before.
The positive (red) wire from the nine volt battery is connected to the motor. The other wire from the motor is connected to the collector pin of the transistor.
Now when the microprocessor turns on the transistor, the current goes from the nine volt battery and into the motor. The battery for the computer is no longer powering the motor. The motor spins much faster, and can actually do some serious work now.
As before, we can control the speed using Pulse Width Modulation.
Connecting the grounds of both power supplies (batteries in this case) gives the computer and the transistor a common reference point, so they both see the three volt signal from the computer pin as higher than ground. With the common reference point, they can agree on what an On or OFF signal is.