What the function of bjt and diode in this circuit
The transistor here is “supposed to” boost the logic current with current gain but as a low side switch. It may only be as low as 10% of hFE as a normal PNP switch and initially the idle motor draws up 10x input current to drive the low resistance (DCR spec) of the motor when Vce=Vce(sat). 

Thus you must know Ohm’s Law , the DCR resistance of motor coil , and the Ron for CMOS 5V Logic. 

Then you must compute voltage attenuation drop.  But this poor design cannot do this very well from a reversed NPN.

This is a low side inverting switch configuration to near 0V (if no load) with motor’s other side  to V+.  But a reverse NPN has almost no current gain (except a few types) i.e. x1

However an NPN can  work backwards as a Low current 0mV switch but possibly fail with hot spots undetected from Vbe reverse =9V (>5max) driving say a 100mA motor with a 1A start Current.

An PNP will work better but with Collector output.**.   The CE junction is designed for high reverse  voltage and much higher current gain used properly, with forward Vbe, while the BE junction in reverse must not avalanche unless you want a Zener with higher capacitance and partial damage from hot spots.

Let me say how a better design works.  Use an NFET or NPN switch which both have a transimpedance gain, gm.

When turned off, from on=low, to high, the motor inductance creates a large LdI/dt=V +ve spike in the opposite direction when  the collector was active ON, which must be clamped the reverse diode D2, to V+. As the motor slows down it now generates a voltage and is shorted by the current thru that diode to V+ till it stops!, with a voltage proportional to speed. If no diode, it might be a very large spike then coast to a stop. 

Thus the diode D2, (clamps the overvoltage) and must handle the same BEMF  current as the transistor for some inertial time response as D2 acts as a dynamic brake.

The low-side diode, D1, serves no purpose unless some external force on  the motor acts in reverse direction  and shunts the output electrical power as a brake according to the mechanical input power.

This explanation made terms you have to look up.!!