In an atom, the charge comes from the protons and electrons. Protons carry a positive charge, while electrons carry a negative charge. The number of protons in an atom determines its atomic number and identifies the element. For example, hydrogen has one proton, helium has two protons, and so on.
By default, the number of protons in an atom is equal to the number of electrons. This balance of positive and negative charges keeps the atom electrically neutral. In other words, the positive charge of the protons is exactly canceled out by the negative charge of the electrons, resulting in a net charge of zero for the atom.
However, it is possible for an atom to gain or lose electrons. When an atom gains electrons, it becomes negatively charged because it now has more electrons than protons. Such an atom is called an anion. Conversely, when an atom loses electrons, it becomes positively charged because it now has fewer electrons than protons. This type of atom is called a cation.
The number of electrons in an atom can change due to various factors, such as chemical reactions, interactions with other atoms or molecules, or the influence of external forces. It is the difference in the number of protons and electrons that gives an atom its charge.