Star formation quenching in simulated group and cluster galaxies: when, how, and why?