The opening and closing of ion channels due to a stimulus. The stimulus can be a change in membrane potential (voltage-gated), drugs or chemical transmitters (ligand-gated), or a mechanical deformation. Gating is thought to involve conformational changes of the ion channel which alters selective permeability.