We present a general semiclassical theory of the orbital magnetic response of noninteracting electrons confined in two-dimensional potentials. We calculate the magnetic susceptibility of singly-connected and the persistent currents of multiply-connected geometries. We concentrate on the geometric effects by studying confinement by perfect (disorder free) potentials stressing the importance of the underlying classical dynamics. We demonstrate that in a constrained geometry the standard Landau diamagnetic response is always present, but is dominated by finite-size corrections of a quasi-random sign which may be orders of magnitude larger. These corrections are very sensitive to the nature of the classical dynamics. Systems which are integrable at zero magnetic field exhibit larger magnetic response than those which are chaotic. This difference arises from the large oscillations of the density of states in integrable systems due to the existence of families of periodic orbits. The connection between quantum and classical behavior naturally arises from the use of semiclassical expansions. This key tool becomes particularly simple and insightful at finite temperature, where only short classical trajectories need to be kept in the expansion. In addition to the general theory for integrable systems, we analyze in detail a few typical examples of experimental relevance: circles, rings and square billiards. In the latter, extensive numerical calculations are used as a check for the success of the semiclassical analysis. We study the weak-field regime where classical trajectories remain essentially unaffected, the intermediate field regime where we identify new oscillations characteristic for ballistic mesoscopic structures, and the high-field regime where the typical de Haas-van Alphen oscillations exhibit finite-size corrections. We address the comparison with experimental data obtained in high-mobility semiconductor microstructures discussing the differences between individual and ensemble measurements, and the applicability of the present model. 03.65.Sq,05.45.+b,05.30.Fk,73.20.Dx