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.