Essentially they are to keep the motor from coming out of the rocket. In some rockets they keep the motor from kicking out when the ejection charge come out, in bigger rockets they keep the motors from falling out due to deceleration or acceleration (if the motors aren't groundstarted)
I just use a screw or some tape. However there are some expensive manufactured retainers that can be bought