We present cosmological constraints from measurements of the gas mass fraction, \(f_{gas}\), for massive, dynamically relaxed galaxy clusters. Our data set consists of Chandra observations of 40 such clusters, identified in a comprehensive search of the Chandra archive, as well as high-quality weak gravitational lensing data for a subset of these clusters. Incorporating a robust gravitational lensing calibration of the X-ray mass estimates, and restricting our measurements to the most self-similar and accurately measured regions of clusters, significantly reduces systematic uncertainties compared to previous work. Our data for the first time constrain the intrinsic scatter in \(f_{gas}\), \((7.4\pm2.3)\)% in a spherical shell at radii 0.8-1.2 \(r_{2500}\), consistent with the expected variation in gas depletion and non-thermal pressure for relaxed clusters. From the lowest-redshift data in our sample we obtain a constraint on a combination of the Hubble parameter and cosmic baryon fraction, \(h^{3/2}\Omega_b/\Omega_m=0.089\pm0.012\), that is insensitive to the nature of dark energy. Combined with standard priors on \(h\) and \(\Omega_b h^2\), this provides a tight constraint on the cosmic matter density, \(\Omega_m=0.27\pm0.04\), which is similarly insensitive to dark energy. Using the entire cluster sample, extending to \(z>1\), we obtain consistent results for \(\Omega_m\) and interesting constraints on dark energy: \(\Omega_\Lambda=0.65^{+0.17}_{-0.22}\) for non-flat \(\Lambda\)CDM models, and \(w=-0.98\pm0.26\) for flat constant-\(w\) models. Our results are both competitive and consistent with those from recent CMB, SNIa and BAO data. We present constraints on models of evolving dark energy from the combination of \(f_{gas}\) data with these external data sets, and comment on the possibilities for improved \(f_{gas}\) constraints using current and next-generation X-ray observatories and lensing data. (Abridged)