The interplay of superconductivity and disorder has intrigued scientists for several decades. Disorder is expected to enhance the electrical resistance of a system, whereas superconductivity is associated with a zero-resistance state. Although superconductivity has been predicted to persist even in the presence of disorder1, experiments performed on thin films have demonstrated a transition from a superconducting to an insulating state with increasing disorder or magnetic field2. The nature of this transition is still under debate, and the subject has become even more relevant with the realization that high-transition-temperature (high-Tc) superconductors are intrinsically disordered3, 4, 5. Here we present numerical simulations of the superconductor–insulator transition in two-dimensional disordered superconductors, starting from a microscopic description that includes thermal phase fluctuations. We demonstrate explicitly that disorder leads to the formation of islands where the superconducting order is high. For weak disorder, or high electron density, increasing the magnetic field results in the eventual vanishing of the amplitude of the superconducting order parameter, thereby forming an insulating state. On the other hand, at lower electron densities or higher disorder, increasing the magnetic field suppresses the correlations between the phases of the superconducting order parameter in different islands, giving rise to a different type of superconductor–insulator transition. One of the important predictions of this work is that in the regime of high disorder, there are still superconducting islands in the sample, even on the insulating side of the transition. This result, which is consistent with experiments6, 7, explains the recently observed huge magneto-resistance peak in disordered thin films8, 9, 10 and may be relevant to the observation of 'the pseudogap phenomenon' in underdoped high-Tc superconductors11, 12.