For charge neutrality, the number of electrons has to equal the number of protons. So, the question really is how to know how many protons and how many electrons. This is actually a very interesting question dating back to the early days (and even before) of nuclear physics and chemistry.
Now, one can do it fairly easily with an ion accelerator and a magnet. With the right set up, one can get a beam of mono-energetic ions out of the accelerator, send them through a stripper to remove electrons, and then send them through a magnet to separate the components out by velocity (which depends on the ion mass through the energy). From the magnet deflections one can get a charge-to-mass ratios, and get back to number of protons (charge on a fully stripped ion) and number of neutrons (from number of protons and the mass). This is the heart of mass spectrometry (although one can use time-of-flight techniques rather than, or in addition to, magnetic separation).