Characterizing hyperbolic spaces and real trees