Characterization of the Hardy property of means and the best Hardy constants