From your diagram, I believe the circuit you have made is what I have drawn in the attachment.
Lets work through what everything does.
The Motor and Diode
A motor is a device which converts electrical energy into mechanical energy. There are many types, but you have a small DC motor. For such a motor, if you apply a sufficient voltage, the motor will spin. Lets say that sufficient voltage is 5v, as that is what you are using in your circuit. If you connect +5v to the red wire, and 0v to the black wire there is 5-0=5V across the motor and so it will spin. If you reverse the red and black leads it will spin in the opposite direction as you now have 0-5=-5V across it.
The problem with these motors is they have some 'inductance' associated with them. The inductance comes from the fact that the motor is setting up a magnetic field which is what is moving the rotor. Now the voltage across an inductor is equal to its inductance multiplied by the rate at which the current flowing through it is changing. If you change the current slowly or not at all, the voltage is very low or zero. But if you change the current very quickly, the voltage is high.
Now consider what happens if you disconnect the supply from the motor instantly (pull a wire out of the breadboard). If the motor had current flowing through it, the current has instantly stopped as you pulled out the wire. This means that the rate of change of current is theoretically infinite. Based on that, the inductance in the motor suddenly has infinite voltage across it. Now infinite voltage sounds a totally daft concept, and thankfully it doesn't happen. Instead you get a very high voltage which causes a spark as current rushes through the air from one terminal of the motor to another. To protect the motor from this arcing, what we can do is put a 'freewheeling diode' or 'flyback diode' across the motor.
A standard diode acts like a one way street. Current can only from the Anode (A) to the Cathode (K), and not the other way around. If you connect the diode as shown in the diagram (ignoring the transistor at the bottom for now), can you see that current will not flow through it? (The anode is at 0V, the cathode at +5V, so it is reverse biased - in other words current wants to flow from the +5v to the 0v, but can't as that would require it to flow from the cathode to the anode). So normally in the circuit below, the diode wont conduct.
So how can we use this to protect the motor? Well, when the supply is removed, the inductor wants to keep the current moving so that it doesn't have infinite voltage across it. As such it drives a current which can now flow from the '-' terminal of the motor, through the diode (going from anode to cathode, the direction a diode conducts), and back to the '+' terminal of the motor. This current decreases as the energy stored in the inductance decreases, until eventually the inductor is discharged and the current stops. Hurray, the supply can be switched off instantly and the motor is protected from sparks.
The Transistor
Basically the transistor is a cross between a switch and an amplifier, and really how it works depends on what transistor type you have. I have drawn a BJT (Bipolar Junction Transistor), such as the 2N2222A or BC547 and many others. I am guessing you have the former as I found a page which references the tutorial I believe you followed.
The way a BJT works is that if a current 'B' flows into the base (and out of the emitter), a larger current 'C' flows into the collect and out of the emitter. These two currents are proportional to each other.So we can say that:
C = H x B
Where H is the 'DC current gain' of the transistor. This value depends on the voltages and currents you are using and is generally not linear, but lets for the say of simplicity say H=180. This is about correct for the value of current I will use below according to the datasheet.
Lets say that the motor requires 77mA to flow through it in order to run. Looking at the circuit, that means we need 77mA to flow from the +5v line, through the motor, and into the collector of the transistor. Based on what we have said about currents in the transistor, this means we need a current flowing into the base of:
B = C/H = 77 / 180 = 0.43mA
There is one more thing that you should know about BJT transistors and that is that in order for it to switch on, you need approximately 0.7V between the Base and the Emitter. That is where the resistor in the diagram comes in.
The voltage across a resistor is defined by 'Ohms Law' as:
V = I x R
Voltage = Current x Resistance.
For this circuit, we need 0.7V across the base, and 0.43mA flowing into it. As such we need a resistor which will converter a Logic 1 of +5v to the values we need.
If we want 0.7V at the transistor, that means the resistor has to drop 5-0.7=4.3V. Furthermore, we need 0.43mA to flow into the base, and in this circuit the only place it can come from is from Pin9, through the resistor and to the base. So we need 0.43mA to flow through the resistor.
Using Ohms Law we get:
R = V/I = 4.3/0.00043 = 10000 Ohms, or 10kOhms.
What happens if we put a logic 0 on pin 9. Well, you will have 0V at pin 9, which means there is 0V dropped across both the resistor and the transistor - think of it as a hill, to top of the hill is at 0V, the bottom of the hill is at ground (which is 0V), so the height is 0.
If there is 0v across the resistor, then you have:
I = V/R = 0/10000 = 0mA flowing into the base.
If there is no current flowing into the base, then there is:
C = 0 * 180 = 0mA flowing into the collector.
No current flowing into the collector means no current flowing through the motor, which means the motor is turned off.
I know I went through that kind of quickly, and there are a lot of words, but if you go over it a couple of times, having a look at the schematic attached, then feel free to ask about anything that is unclear.
(*Disclaimer: some of this post has been over simplified, but that is to make the overall concept of the circuit clearer)