Electron beams are generated using an electron gun, which typically consists of a heated filament that emits electrons through thermionic emission. These electrons are then accelerated using an electric field and focused into a narrow beam using magnetic or electrostatic lenses.