You learn spells through experience. When a spell of level 1+ targets you or you are in the area of a spell's effect, you can take a Reaction to absorb some of that spell's energy. You always have that spell prepared. You can't use this feature to prepare a spell that had no effect on you, even if it forced you to make a saving throw against it.
You can only prepare a spell if it is of a level for which you have spell slots. You can prepare two spells of each level, from level 1-5, for a total of 10 spells prepared. When you prepare a new spell, you can replace a spell of the same level you have prepared with this feature.