Casting spells is different from knowing/preparing spells. The only connection is that when you cast a spell, it must be from those you know or have prepared (with limited exceptions for some rituals). There are 3 tables for how many spellslots a character has to cast with -- 1 for full spellcasters (wizard, cleric, bard, druid, sorcerer), 1 for half casters (rangers and paladins), and 1 for third casters (eldritch knights and arcane tricksters). Warlocks have a different system for this.
Know or preparing spells is different.
Wizards, clerics, druids, and paladins prepare spells -- each has a number of spells they can prepare, and can change them after a long rest. On that day, they can only cast those spells, using their spellslots to do so. Clerics, druids, and paladins choose the spells they have prepared for the day from the entire list of spells; wizards choose from those they have in their spellbooks.
Bards, rangers, warlocks, sorcerers, arcane tricksters, and eldritch knights are much more limited in spell selection. For each of these classes, in their class table it says how many spells the character may know at any given level. When they go up a level, they check that table to see if the number of spells they know goes up; if so, they pick a new spell (or spells if it goes up by more than one) to add. At leveling up, they also have the option to change one spell they know to a new one that they can cast.
When the spellcaster casts a spell, this does not in any way affect which spells he knows or has prepared. Those do not change with casting; each time he casts a spell, he can cast any spell that he knows or has prepared, using a spell slot equal to or greater than the level of the spell he chooses to cast.
So, to answer your request for examples:
A 5th level ranger knows 4 spells. He has just gained the ability to cast 2nd level spells, so he can at most know 2 of those (one for the spell he gained at 5th level, and one could be changed from one of the 1st level spells he knew previously). The other 2 spells he knows will be 1st level spells. He has 4 1st level and 2 2nd level spell slots with which to cast those spells he knows.
A 5th level paladin can prepare a number of spells equal to his charisma modifier, plus 2 for his level. So, for example, if the paladin has a 16 charisma, his modifier is +3, and so at 5th level he can prepare 5 spells. After each long rest, he either keeps the spells he has prepared, or spends a few minutes preparing a different set, from the paladin spell list. He can prepare 1st and 2nd level spells (since that is what he is able to cast), in any combination. He has 4 1st level and 2 2nd level spell slots with which to cast those spells he has prepared.