Here is a very simple model of a gas. A single atom bounces elastically in a one-dimensional cavity. It will continue to bounce at the same speed forever if the piston does not move. If we start the piston compressing, the atom gains speed when it bounces off the moving piston.
That alone is enough to explain why a gas heats when we compress it and cools as we allow it to expand.
Michael Fowler and Qian Xiong, University of Virginia