Answer is: electronegativity.
Electronegativity (χ) is a chemical property that describes the tendency of an atom to attract a shared pair of electrons towards itself.
Atoms with higher electronegativity attracts more electrons towards it, electrons are closer to that atom.
For example, in ionic salt lithium fluoride (LiF), fluorine has electronegativity approximately χ = 4 and lithium χ = 1 (Δχ = 4 - 1; Δχ = 3), fluorine attracts electron and it has negative charge and lithium has positive charge.
Lithium and fluorine are in period 2, lithium is metal far left and fluorine is nonmetal far in the right at the period. Nonmetals have higher tendency to attract shared electrons.