The rear derailleur is responsible for shifting the chain up and down the cogs on the rear cassette. When the rear derailleur fails it makes shifting to and staying on a specific cog difficult, if not impossible. When the derailleur can no longer be adjusted so that it functions properly it is time to replace it with a new one.
Shift the rear derailleur to the smallest cog and leave it in this position throughout this process. Remove the chain from the bike with a chain tool or quick-release link, whichever your chain uses.
Loosen the cable-binding bolt located on the rear derailleur with a hex wrench and remove the cable from the derailleur. Loosen the rear derailleur mounting bolt and remove the derailleur from the bike frame.
Install the new derailleur in the same position as the old derailleur and tighten the mounting bolt to the appropriate torque, based on specifications provided with the derailleur.
Pull the rear derailleur cable tight, place it under the cable binding bolt and tighten the bolt.
Replace the chain, ensuring that it is properly routed.
Adjust the new derailleur by appropriately positioning the body screw, setting the low- and high-gear limit screws, and setting cable tension so the rear derailleur glides smoothly across the rear cogs.