Why does a certain number of moles of gas occupy a set volume?
The reason the gas doesn't expand to infinity is because it is in a vessel.

Or, to be more precise, the ideal gas equation defines the relationship between temperature, pressure and volume for a fixed amount of gas.

What is normally done to understand the relationship is to put the gas into a vessel of fixed volume and measure the pressure it exerts at a given temperature. The pressure changes with temperature according to the formula (real gases may deviate slightly). 

Alternatively, we can use a more complex apparatus that allows us to measure the volume occupied by the gas as we vary the temperature while maintaining a constant pressure (say atmospheric pressure). Same equation, different constraint.

Your misunderstanding is that you have not thought through the conditions under which you are applying the rules. The thought experiment you are doing is releasing the gas in the vacuum of space where there is no external pressure. In those circumstances the gas will expand to infinity but this doesn't violate the ideal gas equation as there is no constraining volume (you could say volume is infinite).

When applying the rules you have three variables (you usually keep the amount of gas, the number of moles, fixed) and you have to constrain two of them. If you don't, are an infinite range of possible combinations of the variables (e.g. constrain temperature and there is still nothing that tells you what the volume and pressure are as they can vary together over any values that satisfy the equation).