Go to the out point of the longest layer of your comp. Hit the n (for eNd) key to define the end of the AE Work Area.
hit the Home key to go to the beginning of the comp. Hit the b (for Begin) key to define the beginning of the AE Work Area.
Now navigate to Composition>Trim Comp To Work Area.
Your comp is now as long as your longest layer. Need it shorter? Go to the point on the AE timeline when you want the comp to end, and repeat the process described above for setting the end of th Work Area, and trimming the comp to the work area.
Thanks a lot for this. Although you could've simply said: "Composition>Trim Comp To Work Area." :-)