Cage length is part of the derailleur design that determines it's total capacity - the ability to take up chain slack when shifting onto small sprockets or chainrings.
If you look at the Shimano rear derailleur specs page you will see that the the 'GS' medium cage models have a higher total capacity than the 'SGS' long cage models. Long cage models are generally for triple chainring setups or cassettes with very large sprocket size spreads.
When looking at a replacement derailleur you need to look at the total capacity and the largest rear sprocket supported. The total capacity needs to be equal to or more than: (difference in tooth count largest and smallest sprockets) + (difference in tooth count largest and smallest chainrings).
It's best to pick the shortest derailleur that meets your needs, the shorter cage has more leverage and tensions the chain better. They also have a lower profile and are less likely to hit branches or rocks or other obstructions on the trail.