Why three quarks?
In very simple terms bound states of quarks (hadrons) have to be color neutral so that means either color quark + anticolor antiquark (mesons) or three quarks carrying R, G and B color charge respectively (baryons). (Note: There should also exist exotic particles like tetraquarks and pentaquarks but these haven't been observed yet and there is quark-gluon plasma which was observed. But none of this exotic matter can play role of a proton)
Now, it turns out that the most stable of all those hadronic particles is proton (which happens to be baryon). Everything else decays to other particles sooner or later (usually very soon) and can't possibly make up the stable matter around us. Neutron makes an exception to this because when it is bound in nucleus it becomes stable (well, not quite, radioactive beta decay can still occur).
Also note that even if some meson particle were stable, mesons are still bosons. So they wouldn't obey Pauli's exclusion principle and it would probably be impossible to build anything like nucleus from them. One very likely needs fermions for that and that means baryons and three quarks.
Size of an atom
This has nothing to do with gravity. Big atoms are unstable purely for nuclear reasons (i.e. their nuclei decay extremely fast via radioactivity). Gravitational considerations are only important for huge objects (typically stars and their afterlife products like white dwarfs and neutron stars).
Mass of a proton
Since Einstein we know that $E=mc^2$, i.e. energy is mass. So anytime there is some interaction you have to account for its binding energy when computing mass of a composite object. Mass of a proton comes almost exclusively from strong interactions that bind the three quarks together.