The spells in your spellbook are the total universe of all spells you can use. The spells you have prepared are the ones you can spend your slots on and cast, right now. You take your spellbook and pick your smaller list of prepared spells every time you rest.
There’s a formula for how many spells you can prepare: your Int mod + wizard level. So, if you have an Int mod of 3 and are level 2, you can have 5 spells prepared. You might have 10, or 15, or 20 in your spellbook to choose from. Every time you wake up from a long rest, you’ve got to take those 20 and winnow them down to the 5 you want to be able to cast.
There’s no mechanical limit on the number of spells you can have in your spellbook. Theoretically you could eventually get access to every single wizard spell in the game. Except for the 2 free spells you get every levelup, the rules don’t provide for any mechanics to ensure you have a steady stream of spells coming into your spellbook. It’s entirely a roleplaying thing you have to work out with your DM.
No other class can learn their entire spell list like this, and it’s one of the wizard’s main strengths. The preparation mechanic is to temper this huge versatility and force you to make choices, but still allow you to be flexible.
To cast a spell as a ritual you can cast it straight from your spellbook, without having it prepared. When you do this, it also does not cost a spell slot. However, it can only be cast at the default level (i.e. you can’t cast it at a higher level for more benefits) and it takes 10 minutes to cast. Ritual spells tend to be utility spells, or at least have a utility function, that normally aren’t time-sensitive, which allows you to prioritize more immediate combat spells on your prepared list.
None of this shit applies to cantrips. You don’t have to copy them into your spellbook, or prepare them, or use slots, or anything. You just always know them and can always cast them whenever you want, and you automatically get more as you level up.
This is a roleplaying thing that the rules just leave it to you to make up. A default assumption is that during offtime your character tinkers and does research and on levelup discover their new spells on their own. But you can take your character concept and explain this however you want.