Electron microscopes provide a powerful platform for exploring physical phenomena with nanoscale resolution, based on the interaction of free electrons with the excitations of a sample such as phonons, excitons, bulk plasmons, and surface plasmons. The interaction usually results in the absorption or emission of such excitations, which can be detected directly through cathodoluminescence or indirectly through electron energy loss spectroscopy (EELS). However, as we show here, the underlying interaction of a free electron and an arbitrary optical excitation goes beyond what was predicted or measured so far, due to the interplay of entanglement and decoherence of the electron-excitation system. The entanglement of electrons and optical excitations can provide new analytical tools in electron microscopy. For example, it can enable measurements of optical coherence, plasmonic lifetimes, and electronic length scales in matter (such as the Bohr radius of an exciton). We show how these can be achieved using common configurations in electron diffraction and EELS, revealing significant changes in the electron's coherence, as well as in other quantum information theoretic measures such as purity. Specifically, we find that the purity after interaction with nanoparticles can only take discrete values, versus a continuum of values for interactions with surface plasmons. We quantify the post-interaction density matrix of the combined electron-excitation system by developing a framework based on macroscopic quantum electrodynamics. The framework enables a quantitative account of decoherence due to excitations in any general polarizable material (optical environment). This framework is thus applicable beyond electron microscopy. Particularly in electron microscopy, our work enriches analytical capabilities and informs the design of quantum information experiments with free electrons, allowing control over their quantum states and their decoherence by the optical environment.