We study the effect of the non-linear process of ambipolar diffusion (joint transport of magnetic flux and charged particles relative to neutral particles) on the long-term behaviour of a non-uniform magnetic field in a one-dimensional geometry. Our main focus is the dissipation of magnetic energy inside neutron stars (particularly magnetars), but our results have a wider application, particularly to the interstellar medium and the loss of magnetic flux from collapsing molecular cloud cores. Our system is a weakly ionized plasma in which neutral and charged particles can be converted into each other through nuclear beta decays (or ionization-recombination processes). In the 'weak-coupling' limit of infrequent inter-particle interactions, the evolution of the magnetic field is controlled by the beta decay rate and can be described by a non-linear partial integro-differential equation. In the opposite, 'strong-coupling' regime, the evolution is controlled by the inter-particle collisions and can be modelled through a non-linear diffusion equation. We show numerically that, in both regimes, ambipolar diffusion tends to spread out the magnetic flux, but, contrary to the normal Ohmic diffusion, it produces sharp magnetic-field gradients with associated current sheets around those regions where the magnetic field is weak. © 2010 The Authors. Journal compilation © 2010 RAS.