I never particularly liked the textbook description of this topic.
The key concept is that there's a quantity called "energy" that we've decided is useful and can't be created or destroyed$^1$, it can just change "types". This is useful because if you choose a system you want to study carefully, you can learn a lot about its behavior from energetic considerations.
Broadly speaking, there are two types of forces, conservative and non-conservative forces. A conservative force is one for which a potential can be defined, and with that potential comes an associated potential energy. For instance, for gravity I can define a potential:
$V(\vec{r}) = -\frac{GM}{||\vec{r}||}$
And there's an associated potential energy:
$U(\vec{r}) = -\frac{GMm}{r} + U_0$
So gravity is a conservative force. The abstraction is that by lifting an object in the gravitational field, I do work and store energy in the field. The field can later release the energy, and no energy is "lost".
Friction is really complicated. It can be, and is, modelled simply, but the process at a microscopic level involves rapidly created and destroyed chemical and/or physical bonds and is not fully understood. When work is done involving friction, we decide not to describe this as energy being stored in some sort of "friction field" (we would need to define a friction potential, and there's no obvious way to do this). Instead, we describe the process by saying that the energy is dissipated by friction, lost as vibration or heat (note that these are both a type of kinetic energy - heat is just a description of the average motions of a collection of particles). The important difference as compared to a conservative force is that heat, for instance, cannot be released from a surface to make a block slide faster. The energy dissipated into heat is "lost" from the system of a block sliding on a rough surface.
With all that in mind, tackling energy conservation problems just takes a bit of practice. My advice would be to forget about the formulae a little bit. Instead, look at the system you're considering and try and account for all the relevant forms of energy, and all possible exchanges/transformations between types of energy. The big "trick" is to define the extent of your system carefully. Students I've taught seem eager to add a thermal energy term to their analysis of problems involving friction, but this is often not a useful exercise. If it's sufficient to know that some energy was dissipated away as heat, you can just include a term in the math that expresses that the system lost, for example, $\mu_kN\Delta x$ of energy.
If I had to break it down into steps, I'd say:
1) Pick the initial state of your system, tally up any potential and kinetic energy.
2) Pick the final state of your system, tally up any potential and kinetic energy.
3) Go through the processes that occur between the initial and final state. Do any of them dissipate energy from the system? Or inject energy?
4) Add everything up (being careful about the sign of each term). Any difference between the initial and final energy of the system should be accounted for by energy injected into or dissipated out of the system in between.
$^1$ At least in simple physics... you can formulate theories/models where energy is created/destroyed, but this is only done if there is some advantage to doing so.
This is a long comment,as to answer your question it needs a course in quantum mechanics.
The basic reason atoms form is because the 1/r Coulomb potential that gives macroscopically the attraction between the positive and negative charges, at the quantum level it gives rise to the atomic orbitals. If there were no quantum mechanics the electrons would fall on the nucleus and neutralize it, and no atoms would exist. It is one of the reasons quantum mechanics had to be postulated.
Orbitals are probability loci in (x,y,z,t) where an electron can be found if measusred.
Here are the possible orbitals for the hydrogen atom.
So the negatively charged electron will be in a specific location (x,y,z) at a specific time .This allows the positive charge of the nucleus range to affect other charges, and the orbitals of different molecules can fit , like lego blocks, positive attracting negative. i.e. a new potential can be found, complicated because of the shielding of the electron orbitals, that can attract molecule to molecule, due to the topology of the orbitals that allows positive electric fields windows of long range.
This new (not 1/r) potential would be too complicated to be solved with the Schrodinger equation, and new mathematical models are used in order to fit and predict the bonding of atoms and molecules, as summarized in the link by Roger
Best Answer
First of all I recommend you to see in Internet Richard Feynman's WHY". It is exactly what he discusses, our questions about why.
An electron in an atom has two major types of energy, kinetic and potential. The first one is due to the fact that the electron performs a motion, e.g. if we calculate the average of the absolute square of the linear momentum of the electron in the ground state of a Hydrogen atom we find $< \hat {P^2} > \ = \ \frac {\hbar ^2}{a_0^2}$,
where $a_0$ is the Bohr radius.
The potential energy comes from the fact that between the electron and proton there exists an electrostatic field.
Now, why does the electron have a movement inside the atom? If the electron were localized at some time to a fix position, the uncertainty principle tells us that its linear momentum could have any value and it is not clear if the electron would remain in the atom.
Where from comes the potential energy: there are a couple of fundamental interactions with which is endowed our universe, and the e.m. interaction is one of them, see in Internet Richard Feynman's WHY. The types I recall are e.m. interaction, strong, weak, and gravitational. As Feynman explains, the existence of these interactions are fundamental axioms of our universe.
By what the energy in atoms (molecules) differs from other energies? Gravitational energy is due to just another type of interaction with which our universe is endowed. Strong interaction is what keeps together the components that constitute the hadrons (protons and neutrons) and the residual strong interaction keeps the hadrons together in the nuclei.