An ideal gas obeys four assumptions: the molecules are in continuous random motion; the volume of the molecules themselves is negligible compared with the volume of the container; there are no attractive forces between the molecules; and all collisions are perfectly elastic, so no kinetic energy is lost. The mean kinetic energy of the molecules is proportional to the absolute temperature.
with $p$ in Pa, $V$ in m$^3$, $T$ in K. For a fixed mass of gas, $\dfrac{p_1V_1}{T_1}=\dfrac{p_2V_2}{T_2}$. Boyle's law ($p \propto 1/V$ at constant $T$) is best tested by plotting $p$ against $1/V$: a straight line through the origin confirms it, whereas a curve of $p$ against $V$ could be almost any inverse relationship.
Real gases
Two assumptions fail. Molecules do attract one another, which pulls them together and makes the measured volume smaller than ideal, so $\dfrac{pV}{nRT}$ falls below $1$ at moderate pressures. Molecules also do occupy volume, and at very high pressure this raises the measured volume above ideal, so $\dfrac{pV}{nRT}$ climbs above $1$. Deviation is greatest at high pressure and low temperature, and least for small, non-polar molecules such as helium.
This graph shows exactly which postulate of the ideal gas fails, and when. The horizontal dashed line at $1$ is the ideal gas, correct at every pressure by definition. The methane curve does three things in order: it dips below $1$, reaches a minimum, then rises above $1$. The dip is due to attraction between molecules: they pull each other together, so the real volume is smaller than the volume an ideal gas would occupy, making the numerator $pV$ smaller. The rise is due to the volume of the molecules themselves: when strongly compressed, the space the molecules occupy can no longer be ignored, so the gas is harder to compress than predicted and $pV$ is larger. The point where the curve crosses $1$ again in the middle does not mean the gas is behaving ideally — it is just where two opposing deviations happen to cancel. The hydrogen curve has no dip because $\ce{H2}$ molecules are so small and nonpolar that the attraction between them is almost negligible; it has only one deviation, and it lies above $1$ from the start.
Worked example: use the ideal gas equation for a mass of gas that changes state
A sealed vessel of volume $2.00$ dm³ contains nitrogen gas at $27\ ^\circ\text{C}$ and $200$ kPa. A valve is opened to let some gas escape, and then the vessel is heated to $127\ ^\circ\text{C}$; the final pressure is $150$ kPa. Calculate the mass of gas that escaped. Take $R=8.31$ J K$^{-1}$ mol$^{-1}$.
Solution
Here the mass of gas changes, so you cannot use $\dfrac{p_1V_1}{T_1}=\dfrac{p_2V_2}{T_2}$ — that formula holds only for a fixed mass of gas. You must use $pV=nRT$ twice, once for each state.
Why you must not use the three-variable formula. If we applied $\dfrac{p_1}{T_1}=\dfrac{p_2}{T_2}$ to this sealed vessel, we would predict a final pressure of $200\times\dfrac{400}{300}=267$ kPa. The real pressure is only $150$ kPa, and that whole difference is the trace of the gas that left the vessel. Recognizing that the detail "the valve is opened" decides the method, not a decorative detail, is the key.
These two graphs are the same data, but only one of them can prove anything. On the left is $p$ plotted against $V$: a falling curve. The problem is that the human eye cannot tell the curve $p=k/V$ from the curve $p=k/V^{1.2}$ or $p=k/\sqrt{V}$ — all three "fall and then flatten out". On the right is $p$ plotted against $\dfrac{1}{V}$: if $p=\dfrac{k}{V}$ then this is the linear relationship $p=k\cdot\dfrac{1}{V}$, so the graph must be a straight line through the origin. A straight line can be checked by eye, and the condition "through the origin" also rules out the form $p=\dfrac{k}{V}+c$. This is a technique reused many times throughout the course: to test a relationship, linearize it before plotting. The kinetics chapter will use exactly this trick when plotting $\ln k$ against $\dfrac{1}{T}$, and the electrochemistry chapter uses it when plotting $E$ against $\lg[\ce{M^{n+}}]$.
Common mistake
Saying that a real gas "behaves ideally" at the point where the $\dfrac{pV}{nRT}$ curve crosses the level $1$ again. At that point the gas is not ideal at all: attraction still exists and pulls the value down, molecular volume still exists and pushes the value up, it is just that the two opposing deviations happen to be equal. The proof is that the position of the crossing point changes with temperature — a real property would not change like that. The twin trap: writing "the gas deviates from ideal because the molecules collide with each other". Collisions are something kinetic theory assumes happen, and that assumption is not wrong; what is wrong are two other assumptions. A correct answer must always name which postulate is violated: attraction between molecules, or the volume of the molecules themselves.
Key takeaway
For gases, every calculation goes through $pV=nRT$ and the three unit conversions. Remember to distinguish two tools: $\dfrac{p_1V_1}{T_1}=\dfrac{p_2V_2}{T_2}$ is for a fixed mass of gas only, while when the amount of gas changes you must use $pV=nRT$ separately for each state. For real gases, always name the correct postulate that is violated.