I don't know about the electromagnetic force, but I think gravitational force is dependant on the mass of both objects.
That's true. It's also true that the electromagnetic force is dependent upon the charge of both objects, in a similar way to the way that gravity is dependent on the masses (in a sense, mass is a "gravitational" charge).
One way to resolve your confusion is to think of orbits. Satellites orbit the Earth, planets orbit the sun, etc., despite the attractive forces between them. This is just because the satellites/planets are moving. They're constantly falling towards the Earth/sun, but they're moving in a perpendicular direction at the same time, so they never quite reach the ground. Electrons move very quickly in their orbits, and the speeds are just enough to keep them from falling into the nucleus.
Another, better way to think about it is to abandon the notion of electrons moving in circular orbits around the nucleus and adopt the quantum theoretic notion of orbitals and wave functions. The orbitals, instead of being regions that the electrons travel through, define the probability that an electron will be found in a given location. These probabilities take into account the electromagnetic attracion between electrons and protons.