The magnetic fields in intermediate-mass and massive stars are incredibly weak compared to what would be expected if flux were frozen into collapsing cloud cores in star-forming regions. Equally puzzling is the enormous range in field strengths detected in these stars, spanning at least five orders of magnitude, as well as an apparent bimodality in the distribution. Moreover, what should we expect the magnetic field to look like in the interior of the star; what shape should it be? How does it remain in a main-sequence star in the absence of any plausible dynamo process? I summarise the current state of our understanding in terms of physical processes during star formation and the pre-main-sequence, magnetohydrodynamic stability and instability, and the often surprising effects of rotation. Finally I take a look at possible origins of the bimodality which sets magnetic stars apart from the rest of the population, for instance via mergers, and how the various scenarios might be tested.