Longitudinal and transverse dynamics of ions from residual gas in an electron accelerator