What keeps electrons from becoming attached to protons in the nucleus?

That is an excellent question. It is in fact one of the key questions that led to the development of quantum mechanics.

Let us consider the hydrogen atom, one proton and one electron. You can reasonably imagine a particle like an electron with an negative charge orbiting a positively charged proton in exactly the same way that a satellite orbits the earth, both the electrostatic attraction and the gravitational attraction being inverse square laws. But an accelerating charged particle (and circular motion with a constant change in direction is an accelerating motion) must radiate energy in accordance with Maxwell's equations. So an electron orbiting a nucleus would be radiating energy, slowing and ought therefore to spiral into the nucleus. But it doesn't and this was a puzzle.

But quantum theory came to the rescue to provide an explanation. de Broglie postulated that all moving objects, like the electron orbiting the nucleus have an associated wavelength.

Bohr proposed that an electron orbiting a nucleus could only have an orbit in which the circumference was an integral number of wavelengths. This implies that there are only discrete energies that are allowable for the electron orbiting the nucleus, and therefore it cannot continually radiate energy away. This theory accurately predicts the emission spectra for hydrogen and provides an explanation for why the atom is stable.

*from a Space.com forum, post by DrRocket