For a covalent bond (like between carbon atoms) they share electrons between them. The shared electrons are the glue between atoms.
For ionic bonds, electrons transfer from one to the other, leaving one positively charged and the other negatively charged, and then the opposite electric charges attract. So if you start with a sodium atom (Na) and a chlorine atom (Cl), an electron jumps from the sodium, leaving it as the Na+ ion and goes to the chloride, leavign it as the Cl- ion. THen those are attracted to make salt crystals.
Whether you form a covalent bond or an ionic bond between two atoms depends on how much the two atoms differ in their desire for electrons. Chlorine loves to get one additional electron and sodium loves to give up one electron, so they form ionic bonds. Carbon doesn't care that much either way, so shares with other atoms like carbon, oxygen, nitrogen, etc.