Robert Boyle, an Irish chemist who lived from 1627 to 1691, was the first person to relate the volume of a gas in a confined space to the volume it occupies. He found that if you increase the pressure (P) on a fixed amount of gas at a constant temperature, the volume (V) decreases in such a way that the product of pressure and volume remain constant. If you lower the pressure, volume increases. In mathematical terms: PV = C, where C is a constant. This relationship, known as Boyle's Law, is one of the cornerstones of chemistry. Why does this happen? The usual answer to that question involves conceptualizing a gas as a collection of freely-moving microscopic particles.
TL;DR (Too Long; Didn't Read)
The pressure of a gas varies inversely with volume because the gas particles have a constant amount of kinetic energy at a fixed temperature.
An Ideal Gas
Boyle's Law is one of the precursors of the ideal gas law, which states that PV = nRT, where n is the mass of the gas, T is the temperature and R is the gas constant. The ideal gas law, like Boyle's Law, is technically only true for an ideal gas, although both relationships provide good approximations to real situations. An ideal gas has two characteristics that never occur in real life. The first is that the gas particles are 100 percent elastic, and when they strike each other or the walls of the container, they don't lose any energy. The second characteristic is that ideal gas particles occupy no space. They are essentially mathematical points with no extension. Real atoms and molecules are infinitesimally small, but they do occupy space.
What Creates Pressure?
You can understand how a gas exerts pressure on a the walls of a container only if you don't make the assumption that they have no extension in space. A real gas particle has not only mass, it has energy of movement, or kinetic energy. When you put a large number of such particles together in a container, the energy they impart to the walls of the container creates pressure on the walls, and this is the pressure to which Boyle's Law refers. Assuming the particles to be otherwise ideal, they will continue to exert the same amount of pressure on the walls as long as the temperature and total number of particles remain constant, and you don't modify the container. In other words, if T, n and V are constant, then the ideal gas law (PV = nRT) tells us that P is constant.
Alter Volume and You Alter Pressure
Now suppose you allow the volume of the container to increase The particles have farther to go in their journey to the container walls, and before reaching them are likely to suffer more collisions with other particles. The overall result is that fewer particles hit the container walls, and those that make it have less kinetic energy. Although it would be impossible to track individual particles in a container, because they number in the order of 1023, we can observe the overall effect. That effect, as recorded by Boyle and thousands of researchers after him, is that the pressure on the walls goes down.
In the reverse situation, particles get crowded together when you decrease the volume. As long as the temperature stays constant, they have the same kinetic energy, and more of them hit the walls more frequently, so the pressure goes up.