We report on the design, construction, and performance of a compact magnetic shield that facilitates a controlled, low-noise environment for experiments with ultracold atomic gases. The shield was designed to passively attenuate external slowly-varying magnetic fields while allowing for ample optical access. The geometry, number of layers and choice of materials were optimised using extensive finite-element numerical simulations. The measured performance of the shield is in good agreement with the simulations. From measurements of the spin coherence of an ultracold atomic ensemble we demonstrate a remnant field noise of 2.6 microGauss and a suppression of external dc magnetic fields by more than five orders of magnitude.