We developed a code that estimates distances to stars using measured spectroscopic and photometric quantities. We employ a Bayesian approach to build the probability distribution function over stellar evolutionary models given these data, delivering estimates of model parameters for each star individually. The code was first tested on simulations, successfully recovering input distances to mock stars with <1% bias.The method-intrinsic random distance uncertainties for typical spectroscopic survey measurements amount to around 10% for dwarf stars and 20\% for giants, and are most sensitive to the quality of $\log g$ measurements. The code was validated by comparing our distance estimates to parallax measurements from the Hipparcos mission for nearby stars (< 300 pc), to asteroseismic distances of CoRoT red giant stars, and to known distances of well-studied open and globular clusters. The external comparisons confirm that our distances are subject to very small systematic biases with respect to the fundamental Hipparcos scale (+0.4 % for dwarfs, and +1.6% for giants). The typical random distance scatter is 18% for dwarfs, and 26% for giants. For the CoRoT-APOGEE sample, the typical random distance scatter is ~15%, both for the nearby and farther data. Our distances are systematically larger than the CoRoT ones by about +9%, which can mostly be attributed to the different choice of priors. The comparison to known distances of star clusters from SEGUE and APOGEE has led to significant systematic differences for many cluster stars, but with opposite signs, and with substantial scatter. Finally, we tested our distances against those previously determined for a high-quality sample of giant stars from the RAVE survey, again finding a small systematic trend of +5% and an rms scatter of 30%.