safety for power electronics
Connecting the grounds together is required since without that there would be no circuit for the current to flow through.  Take this simple block diagram for instance:



In the top diagram there are two complete circuits - the green one (Arduino's 5V power) and the blue one (device's 12V power).  The communication between the Arduino and the Device can't happen because there is no circuit (red).

In the bottom diagram the red circuit has been completed by joining the grounds together.  That means that the current has some way to get back to where it came from (which current always has to do) so the signals you send can actually get through.

To avoid having to connect the grounds together you need something called Galvanic Isolation.  This involves using some method that is not electricity to connect the two circuits together.  That could be using light in the form of an opto-coupler, or magnetism in the form of a transformer, or even something even more physical like a relay.  In that situation you have an arrangement like this:



The ground connection that was part of the red circuit above has now been sliced in half (red and orange now) and the isolation device is responsible for passing the signal from one side to the other.

Galvanic isolation is highly desirable when:


You are controlling a different type of current (e.g., using DC to switch AC)
You are working with voltages considerably higher than your drive voltage (e.g., switching mains)
You have different ground potentials (e.g., you have a long distance connection)