In fact the electrons (at least those in s-shells) do spend some non-trivial time inside the nucleus.
The reason they spend a lot of time outside the nucleus is essentially quantum mechanical. To use too simple an explanation their momentum is restricted to a range consistent with being captured (not free to fly away), and as such there is a necessary uncertainty in their position.
An example of physics arising because they spend some time in the nucleus is so called "beta capture" radioactive decay in which
$$ e + p \to n + \nu $$
occurs within the nucleus. The reason this does not happen in most nuclei is also quantum mechanical and is related to energy levels and Fermi-exclusion.
To expand on this picture a little bit, let's appeal to de Broglie and Bohr. Bohr's picture of the electron orbits being restricted to a set of finite energies $E_n \propto 1/n^2$ and frequencies can be given a reasonably natural explanation in terms of de Broglie's picture of all matter as being composed of waves of frequency $f = E/h$ by requiring that a integer number of waves fit into the circular orbit.
This leads to a picture of the atom in which all the electrons occupy neat circular orbits far away from the nucleus, and provides one explanation of why the electrons don't just fall into the nucleus under the electrostatic attraction.
But it's not the whole story for a number of reasons; for our purposes the most important one is that Bohr's model predicts a minimum angular momentum for the electrons of $\hbar$ when the experimental value is 0.
Pushing on, we can solve the three dimensional Schrödinger equation in three dimensions for Hydrogen-like atoms:
$$ \left( i\hbar\frac{\partial}{\partial t} - \hat{H} \right) \Psi = 0 $$
for electrons in a $1/r^2$ electrostatic potential to determine the wavefunction $\Psi$. The wave function is related to the probability $P(\vec{x})$ of finding an electron at a point $\vec{x}$ in space by
$$ P(\vec{x}) = \left| \Psi(\vec{x}) \right|^2 = \Psi^{*}(\vec{x}) \Psi(\vec{x}) $$
where $^{*}$ means the complex conjugate.
The solutions are usually written in the form
$$ \Psi(\vec{x}) = Y^m_l(\theta,\phi) L^{2l+1}_{n-l-1}(r) e^{-r/2} * \text{normalizing factors} $$
Here the $Y$'s are the spherical harmonics and the $L$'s are the generalized Laguerre polynomials. But we don't care for the details. Suffice it to say that these solutions represent a probability density for the electrons that is smeared out over a wide area near around the nucleus. Also of note, for $l=0$ states (also known as s orbitals) there is a non-zero probability at the center, which is to say in the nucleus (this fact arises because these orbital have zero angular momentum, which you might recall was not a feature of the Bohr atom).