Two-dimensional direct numerical simulation of the flow over a NACA0018 airfoil with a cavity is presented. The low Reynolds number simulations are validated by means of flow visualizations carried out in a water channel. From the simulations, it follows that there are two main regimes of flow inside the cavity. Depending on the angle of attack, the first or the second shear-layer mode (Rossiter tone) is present. The global effect of the cavity on the flow around the airfoil is the generation of vortices that reduce flow separation downstream of the cavity. At high positive angles of attack, the flow separates in front of the cavity, and the separated flow interacts with the cavity, causing the generation of smaller-scale structures and a narrower wake compared with the case when no cavity is present. At certain angles of attack, the numerical results suggest the possibility of a higher lift-to-drag ratio for the airfoil with cavity compared with the airfoil without cavity.