Protons do repel electromagnetically. In the nucleus however, this is counter-balanced by the strong nuclear force. Quantum chromodynamics (QCD) -
In theoretical physics, quantum chromodynamics (QCD) is the theory of the strong interaction between quarks and gluons, the fundamental particles that make up composite hadrons such as the proton, neutron and pion.
From our current knowledge of the physical world, particles can intereact with each others thanks to 4 differents "interaction forces":
- Gravity, that attracts particles on their energy content;
- Electromagnetism, that attracts or repels particles based on their electric charge;
- Strong nuclear force, that attracts particles based on their strong nuclear charges, a.k.a. "color charges";
- Weak nuclear force, that makes neutrons unstable so they transform into protons (ok, at this level of discussion, this one seems like a weird "force", but that is all you need to know!)
So
YES, protons do repel each others in the nucleus due to the positive electric charges they posses, BUT, they also are massive particles and they possess color charges. And the same holds for neutrons: massive and color charge.
From
gravity, protons and neutrons do attract each others, but the gravity force is so weak compare to the protons' electromagnetic repulsion that its effect is irrelevant.
On the other hand, the
strong nuclear force is a strong force (compare to electromagnetism), but only has effect at very short distances, about the size of the atom nucleus. And this is what hold the proton (and the neutron) together.
Note that the electrons do not have color charges, which is why they espace from the nucleus, but are bounded to it due to the electromagnetism attraction.
But the strong force has its limits against the electromagnestim repulsion. This is why the neutrons are important for large nucleus (with large number of protons) to counter the electromagnetic repulsion.
Note that too many neutrons make the nucleus more likely to be unstable (radioactive) due to the weak nuclear force.