There really is no short explanation for this except to familiarize/memorize the commons ions and their charges based on the periodic trends.
For (a) the ions present are
For (b) the ions present are
Of course, both chemical bondings can be explained by the Lewis Dot Structure (Octet Rule), which is defined as a chemical rule that states atoms tend to combine in such a way that each atom has the same electronic configuration as a noble gas.
Let's take a look at the electronic configuration of potassium (K), with atomic number 19.
For potassium to mimic the electronic configuration of the nearest noble gas (in this case Argon, atomic number 18), K needs to lose the one electron in its outermost shell in order to become stable.
The same can be said with
Since the potassium is more than willing to give up its electron, sulfur can accept two electrons (one from each two potassium atoms) to get the same electronic configuration as Argon.
The two ions are in a give-and-take electron relationship, which makes it an ionic bond.
For (b), it is much more complicated since both nitrogen and fluoride cannot afford to give or take electrons from each other to completely mimic the electron configuration of noble gases - which is why they choose to share electrons instead, making it a covalent bond .