How do I calculate the length of a ladder I need to reach a certain height?
You have to take into account a few factors:  


extension ladders have an overlap that isn't shown in the nominal length - for example, a 16' extension ladder has two 8' sections, but fully extended is only 13' long.  


the ladder will be used at an angle (usually a four to one pitch / 75 degrees) so the top of a 16' ladder won't even be at 13' in use
you don't want to stand on the top few rungs
some tasks take more ladder than others, if you're painting, you can reach up and work a little higher than the top of the ladder, but if you have to get on a roof, you need the ladder that extends well past the edge of the roof (usually about three feet)



Trig tells us the height of the top of a ladder tilted at 75 degrees will be .97 the length.  For example, a 16' extension ladder (which is really 13' long extended) set up at a 4:1 / 75 degree pitch will touch a wall at about 12'7" (13' * .97).  

This chart has all you need to know, no math: