Context. Hydrides are simple compounds containing one or a few hydrogen atoms bonded to a heavier atom. They are fundamental precursor molecules in cosmic chemistry and many hydride ions have become observable in high quality for the first time thanks to the Herschel Space Observatory. Ionized hydrides such as CH + and OH + (and also HCO + ), which affect the chemistry of molecules such as water, provide complementary information on irradiation by far-UV (FUV) or X-rays and gas temperature. Aims. We explore hydrides of the most abundant heavier elements in an observational survey covering young stellar objects (YSOs) with different mass and evolutionary state. The focus is on hydrides associated with the dense protostellar envelope and outflows, contrary to previous work that focused on hydrides in diffuse foreground clouds. Methods. Twelve YSOs were observed with HIFI on Herschel in six spectral settings providing fully velocity-resolved line profiles as part of the Water in star-forming regions with Herschel (WISH) program. The YSOs include objects of low (Class 0 and I), intermediate, and high mass, with luminosities ranging from 4 L to 2 × 10 5 L . Results. The targeted lines of CH + , OH + , H 2 O + , C + , and CH are detected mostly in blue-shifted absorption. H 3 O + and SH + are detected in emission and only toward some high-mass objects. The observed line parameters and correlations suggest two different origins related to gas entrained by the outflows and to the circumstellar envelope. The derived column densities correlate with bolometric luminosity and envelope mass for all molecules, best for CH, CH + , and HCO + . The column density ratios of CH + /OH + are estimated from chemical slab models, assuming that the H 2 density is given by the specific density model of each object at the beam radius. For the low-mass YSOs the observed ratio can be reproduced for an FUV flux of 2-400 times the interstellar radiation field (ISRF) at the location of the molecules. In two high-mass objects, the UV flux is 20-200 times the ISRF derived from absorption lines, and 300-600 ISRF using emission lines. Upper limits for the X-ray luminosity can be derived from H 3 O + observations for some low-mass objects. Conclusions. If the FUV flux required for low-mass objects originates at the central protostar, a substantial FUV luminosity, up to 1.5 L , is required. There is no molecular evidence for X-ray induced chemistry in the low-mass objects on the observed scales of a few 1000 AU. For high-mass regions, the FUV flux required to produce the observed molecular ratios is smaller than the unattenuated flux expected from the central object(s) at the Herschel beam radius. This is consistent with an FUV flux reduced by circumstellar extinction or by bloating of the protostar.