The stability of carbocations is affected by neighboring carbon atoms, neighboring lone pairs and carbon-carbon multiple bonds. When a carbon atom forms a bond with three carbon atoms, it results in carbocation.
Carbocations have positive charge. As the number of positively charged carbon atoms decreases, the stability of the carbocation decreases. The number of carbon atoms in the carbocation is directly proportional to carbocation stability.
When a carbon atom comes close to a carbocation, the electrons are donated to the carbocation ion. As soon as the electron is donated to the positive ion, it becomes stable. The more electrons it receives from its neighboring carbon atoms, the more stable it gets.
Similarly, lone pairs act as electron donors when they come close to carbocation ions. Oxygen and nitrogen are the most prominent lone pairs that readily donate electrons to carbocations, thus increasing the stability of the ion.
Carbon-carbon atoms are more stable than carbocations. When a carbocation is bonded with multiple carbon atoms, its stability increases. As soon as such a carbon-carbon atom approaches a carbocation ion, it instantly donates the electron to it. Carbocations are stabilized when they get electrons added to their orbit. This all occurs because opposite charges attract, and like charges repel.