We combine scanning tunneling microscopy (STM) measurements with ab initio calculations to study the self-assembly of long chain alkanes and related alcohol and carboxylic acid molecules on graphite. For each system, we identify the optimum adsorption geometry and explain the energetic origin of the domain formation observed in the STM images. Our results for the hierarchy of adsorbate-adsorbate and adsorbate-substrate interactions provide a quantitative basis to understand the ordering of long chain alkanes in self-assembled monolayers and ways to modify it using alcohol and acid functional groups.