Spicules are dynamic jets propelled upwards (at speeds of ~20kms-1) from the solar `surface' (photosphere) into the magnetized low atmosphere of the Sun. They carry a mass flux of 100 times that of the solar wind into the low solar corona. With diameters close to observational limits (< 500km), spicules have been largely unexplained since their discovery in 1877: none of the existing models can account simultaneously for their ubiquity, evolution, energetics and recently discovered periodicity. Here we report a synthesis of modelling and high-spatial-resolution observations in which numerical simulations driven by observed photospheric velocities directly reproduce the observed occurrence and properties of individual spicules. Photospheric velocities are dominated by convective granulation (which has been considered before for spicule formation) and by p-modes (which are solar global resonant acoustic oscillations visible in the photosphere as quasi-sinusoidal velocity and intensity pulsations). We show that the previously ignored p-modes are crucial: on inclined magnetic flux tubes, the p-modes leak sufficient energy from the global resonant cavity into the chromosphere to power shocks that drive upward flows and form spicules.