In the application you linked, the transistor is part of two circuits, so we need a common reference between the circuits to enable the transistor to operate correctly.
simulate this circuit – Schematic created using CircuitLab
If there was no connection between points A and B, the microcontroller could not control the transistor bewcause there would be no path for the base current from the microcontroller to get back to the microcontroller's power supply.
If the connection between points B and C was removed, the relay would not operate, regardless of the state of the transistor, because there would be no path for the relay coil current to make it back to the 12 volt battery.
Having the common ground connection between the two power supplies allows the circuit to operate as required.
It would be possible to have a scenario where the positive terminals of the supplies have to be connected instead of the negative - it all depends on where you need a common reference point.
...edit after revised question...
Diode D1 is a flyback or spike suppression diode. When the transistor turns off, the magnetic field in the motor or relay (or other inductive device) will colllapse rapidly, and would create a high voltage spike on the transistor's collector. The diode will conduct the current that the collapsing field generates, preventing the voltage spike from forming.