As long as you want.
As with anything, there are considerations:
Hop Flavor and Aroma
This is a big one. Hop compounds break down and dissipate extremely quickly. If you want a fresh hop flavor and/or aroma in the finished product you need to serve the beer ASAP. Age doesn't impact the bittering nearly as much, however.
The second consideration with hops is that you do not want your beer to become lightstruck (skunked.) The carboy should be stored in a dark location to avoid that smelly rodent from rearing it's ugly head.
Yeast Autolysis & Astringency - Racking off of the trub
If you plan on extended aging in a carboy, you want as little trub as possible. Aged trub will contain dead yeast cells (which cause meaty & brewers yeasty vitamin flavors) and hop particles (which can cause an oversteeped teabag astringency that can completely kill a beer.)
Oxidation
For extended aging, you want to use the smallest carboy you can fit your liquid into. This reduces the liquid's total surface area exposed to oxygen (and total head space - which reduces the total oxygen possible.) Preferably, you want to remove the oxygen somehow (by adding a layer of CO2, for example) because this is going to have a big impact on flavor quite quickly. For some styles, such as an English Old Ale, this is okay because oxidation does miraculous things to the malt character. For most styles, however, the last thing you want is a mouth full of wet cardboard.
Biological Contaminations?
Others have pointed this out, but honestly I don't think it it is as big of a factor. Contaminations work pretty quick and if your beer is contaminated it probably won't matter how long you age it. After a couple weeks you will taste it.
That said, if you are trying to contaminate the beer on purpose, that's a whole other story. Assuming the bacteria has something to eat and the alcohol & hops aren't too potent, age helps instead of hindering in production. Bugs turn things on their head though... for example you need oxygen for Brett to prosper.