We present K2SC (K2 Systematics Correction), a Python pipeline to model instrumental systematics and astrophysical variability in light curves from the K2 mission. K2SC uses Gaussian process regression to model position-dependent systematics and time-dependent variability simultaneously, enabling the user to remove both (e.g., for transit searches) or to remove systematics while preserving variability (for variability studies). For periodic variables, K2SC automatically computes estimates of the period, amplitude and evolution timescale of the variability. We apply K2SC to publicly available K2 data from campaigns 3--5, showing that we obtain photometric precision approaching that of the original Kepler mission. We compare our results to other publicly available K2 pipelines, showing that we obtain similar or better results, on average. We use transit injection and recovery tests to evaluate the impact of K2SC on planetary transit searches in K2 PDC (Pre-search Data Conditioning) data, for planet-to-star radius ratios down Rp/Rstar = 0.01 and periods up to P = 40 d, and show that K2SC significantly improves the ability to distinguish between correct and false detections, particularly for small planets. K2SC can be run automatically on many light curves, or manually tailored for specific objects such as pulsating stars or large amplitude eclipsing binaries. It can be run on ASCII and FITS light curve files, regardless of their origin. Both the code and the processed light curves are publicly available, and we provide instructions for downloading and using them. The methodology used by K2SC will be applicable to future transit search missions such as TESS and PLATO.