Molecular-dynamics simulations are used to investigate lateral friction in contact-mode atomic force microscopy of tetracosane (n-C24H50) films. We find larger friction coefficients on the surface of monolayer and bilayer films in which the long axis of the molecules is parallel to the interface than on a surface of molecules with the long axis perpendicular to the surface, in agreement with experimental results. A major dissipation mechanism is the molecular flexibility as manifested in the torsional motion about the molecules' C-C bonds. The generation of gauche defects as a result of this motion does not appear to be in itself a major channel of energy dissipation. As previously reported in the literature, the layer density and thereby the strength of the attractive film-tip interaction is also an important factor in energy dissipation.