Limiting current with a shunt resistor
A shunt resistor (despite the name*) is a resistor in series with the load. By measuring the voltage across a shunt resistor (which should be as low as practical to avoid wasting power in the resistor and depriving the motor of voltage) you can determine the current. There are also DC current sensors that use other principles.
Once you have a current measurement (or a binary indication that it has exceeded a limit) then you can deal with it one way or another. If the current needs to be cut off very promptly, many MCU PWM peripherals have an optional hardware input that can be configured to shut down the output virtually instantly, for just this purpose.
* I assume that they are called &quot;shunt&quot; resistors because they would have been connected across a meter movement.