Consider a system (S) consisting of some quantity of gas enclosed in a cylinder fitted with a movable, massless, and frictionless piston.

Internal energy of a system can be increased by heating the system.
Suppose the gas is heated using a burner (source of heat, environment). Let TS = temperature of the system (gas) and TE = temperature of the environment.
Here, TE > TS . Hence, there will be a net flow of energy (heat) from the environment to the system causing the increase in the internal energy of the system.
The internal energy of the gas (system) can also be increased by quickly pushing the piston inward so that the gas is compressed.

Internal energy of a system can be increased by doing work on the system
The work done on the gas raises the temperature of the gas. Thus, there is increase in the internal energy of the gas. If the gas pushes the piston outward, the work is done by the gas on the environment and the gas cools as its internal energy becomes less.