We propose a chemo-mechanical model based on stress-dependent recruitment of myosin motors to describe how the contractility, polarization and strain in cells vary with the stiffness of their surroundings and their shape. A contractility tensor, which depends on the distribution of myosin motors, is introduced to describe the chemical free energy of the cell due to myosin recruitment. We explicitly include the contributions to the free energy that arise from mechanosensitive signalling pathways (such as the SFX, Rho-Rock and MLCK pathways) through chemo-mechanical coupling parameters. Taking the variations of the total free energy, which consists of the chemical and mechanical components, in accordance with the second law of thermodynamics provides equations for the temporal evolution of the active stress and the contractility tensor. Following this approach, we are able to recover the well-known Hill relation for active stresses, based on the fundamental principles of irreversible thermodynamics rather than phenomenology. We have numerically implemented our free energy-based approach to model spatial distribution of strain and contractility in (i) cells supported by flexible microposts, (ii) cells on two-dimensional substrates, and (iii) cells in three-dimensional matrices. We demonstrate how the polarization of the cells and the orientation of stress fibres can be deduced from the eigenvalues and eigenvectors of the contractility tensor. Our calculations suggest that the chemical free energy of the cell decreases with the stiffness of the extracellular environment as the cytoskeleton polarizes in response to stress-dependent recruitment of molecular motors. The mechanical energy, which includes the strain energy and motor potential energy, however, increases with stiffness, but the overall energy is lower for cells in stiffer environments. This provides a thermodynamic basis for durotaxis, whereby cells preferentially migrate towards stiffer regions of the extracellular environment. Our models also explain, from an energetic perspective, why the shape of the cells can change in response to stiffness of the surroundings. The effect of the stiffness of the nucleus on its shape and the orientation of the stress fibres is also studied for all the above geometries. Along with making testable predictions, we have estimated the magnitudes of the chemo-mechanical coupling parameters for myofibroblasts based on data reported in the literature.