[...] Why don't all the electrons stay in the same energy levels [...] why two or eight electrons are able to stay together.
It is a behaviour of the fermions like electrons, protons, neutrons, mu, tau... as opposed to the bosons like photons. Only one electron can be in one state. This is a mathematical necessity, not a matter of repulsion. And this rule is even stronger, as it even imposes relations between the states occupied by electrons (or rather, it imposes a condition on the one wave function that describes the set of electrons: that's more stringent than "different states").
As the electron's state includes the spin which can have two values, at most two electrons can occupy the same orbital, for instance 1s, 2s...
Now, some orbitals are degenerate. 2px
have the same shape and energy, only with different orientations. Each can hold two electrons of different spin, just like the the 2s. Together, they can hold 3*2+2=8 electrons.
Then the 3s accommodates a pair, three 3p a pair each, and five 3d a pair each, totalling 18 electrons. To 4s 4p 4d comes 4f as well, and so on - this tells why the "periodic" table isn't periodic but rather pyramid-shaped.
Less simple is that these many orbitals have energies that don't organize simply because 1, 2, 3, 4... aren't well separated. For instance once all 3p are filled you get a rare gas, argon, before the 4s and 3d begin to fill, and because 3d and 4s have similar energies they fill in no simple sequence: this makes the first set of transition elements from scandium to zinc.
Here's a table that gives the orbital filling for each element:https://www.nist.gov/sites/default/files/documents/pml/data/Periodic_Table_nocrop_2014.pdf
You can see there element tables with other shapeshttps://en.wikipedia.org/wiki/Periodic_table