Why is a resistor put before the gate of a transistor?
The reasons are different depending on if it is a BJT or a MOSFET.

The effect, though, is the same - it's there to reduce current and protect the IO pins on the controller that are driving the transistors.

On the BJT the base -> emitter junction is essentially like a diode, and without a resistor would be like a near short circuit.  The resistor stops the transistor drawing too much current from the IO pin.

On the MOSFET the gate is like a capacitor.  When you transition from a LOW to a HIGH the capacitor has to charge up.  This charging of the capacitor causes a current spike which quickly decays.  The resistor is there again to protect the IO pin from this current spike.

The value of the resistor on the BJT is not particularly critical, as long as it allows enough current to flow from the collector to the emitter to drive the load.  On the MOSFET, however, selection is more critical, as the resistor, coupled with the capacitance of the gate, creates a low pass filter as the resistor slows down the charging of the capacitor.  The resistor must be low enough to allow the MOSFET to switch fast enough for your signals (e.g., PWM) yet high enough for the current to not endanger the IO pin.