What role does this diode play?
This diode is to surpress any back-EMF caused when the motor is switched off. In general, when one has an inductive load such as a motor, or an electromagnet's solenoid, when you switch it on, there will be an initial drop in current, as some of the current will act to form a magnetic field around the coil. Inversely, when switching off, this magnetic field that has been created needs to dissipate. When there is no back-EMF diode in place, the path would be through the BJT, which would almost certainly damage it, or perhaps other components depending on the circuit.
As for the polarity of the diode itself, when passing current through one way, you generate a field in that respective direction. When you stop the source, that field collapses back to its "rest" position, which means the current will flow the other way momentarily.
All reactive (capacitive and inductive) loads have this kind of "storage" characteristic which needs to be accounted for in design, resistive loads are the exception. If you want to know more about the governing equations and such, wikipedia is a good place to start, or for a good read, try "The Art of Electronics", Horowitz and Hill, 3rd edition.
The motor is an INDUCTIVE LOAD.
Due to Faraday's Law of Induction stating that a time varying/changing current creates a magnetic field with a magnitude directly proportional to the change in the current through the conductor over time and ( as much symmetry exists in physics) a changing magnetic field creates an electric field (a voltage difference) surrounding the conductor that manifests as an opposition to the change in current that created the magnetic field. This is due to Lenz's Law which complete Faraday's Formula for electromagnetic Induction where an electro-motive-force is created equal to the rate of change in the magnetic field over time (which was cause by the change in the flow of current.
Faraday's Law: back-EMF = (-1)dB/dtN where back-EMF is the voltage potential opposite the current flow creating the resistance to change, "-1" is Lenz's Law, "dB" is the change in magnetic flux, and "dT" is the time period over which the change is is measured, and N is how many coils of wire are within the changing electric field.
Your motor is inductive due to the many coils of wire. When it starts up it slowly gains speed instead of instantly being at top speed because of Lenz's law making the back-EMF resist the change in current flow until the current flow is no longer changing and at its maximum. There is now energy stored in the corresponding magnetic field. When you turn off the motor it will still spin and now instead of consuming power it is generating power. The original back EMF flowed toward the supply but now as the motor slows down the inductance will resist the change in current and will force a current to flow forward and into the transistors collector.
Since current is the flow of electrons the electrons must come from somewhere. Your transistor connects the motor to GROUND where it was sourcing electrons initially. The electrons "moved" by the electro-motive force induced by the collapsing magnetic field would bunch up at the transistor collector without the diode and would have to be sourced from your Power Supply which wont like that. With a diode giving a return path for that EMF it will dissipate through the diode and motor after a couple loops through it.
So, the fly back diode allows a path for the electrons to flow around the motor and not into the power supply or the transistor (causing potential damage), created by self induction in the motors windings when turned off and caused by the sudden change in current to zero.