It's an interesting question to ask. Electrons and the nucleus of an atom have opposite charges and one of the important rules of our universe is that opposite charges attract. Yet electrons "orbit" the nucleus at set distances - how can this be so?
The answer is actually best illustrated by flipping the question on its head: why doesn't an electron just escape the atom and fly off into space? Answer: the opposite charge of the nucleus holds it in place. Electrons don't just have charge, they have all sorts of other properties, one of them being energy. The more energy an electron has, the more it can spread out its location i.e. the further away from a nucleus it can get. High energy electrons break free of their atom and go zipping off into the Universe. So really, think of it as an electron's tendency to leave the nucleus. It's constantly trying to escape, but the positive nuclear charge is holding it in place so that it can't escape.