We discuss results from numerical simulations of star formation under various environmental conditions ranging from the turbulent interstellar medium to low-mass halos in the early universe. The thermodynamic behavior of the star-forming gas plays a crucial role in fragmentation and determines the stellar mass function as well as the dynamic properties of the nascent stellar cluster. The thermodynamic state of the gas is a result of the balance between heating and cooling processes, which in turn are determined by atomic and molecular physics and by chemical abundances. Features in the effective equation of state of the gas, such as a transition from a cooling to a heating regime, define a characteristic mass scale for fragmentation and so set the peak of the initial mass function of stars (IMF). As it is based on fundamental physical quantities and constants, this is an attractive approach to explain the apparent universality of the IMF in the solar neighborhood as well as the transition from purely primordial high-mass star formation to the low-mass mode observed today.