Any spells that are prepared can be written down in a spellbook or scroll. To transcribe a spell into your spellbook, you must first prepare the spell from another source, such as a scroll or another spellbook. If the spell is new to you, you must make an Arcana check (DC = 15 + spell level) in order to be able to prepare it for the first time. If you fail, you cannot try again until after you’ve had a long rest.
Wizards and Magi gain new spells every time they gain a level. These spells can be automatically transcribed into your spellbook. They don’t require preparation or an Arana check.
If a library, arcane school, magical archive, or other source of written knowledge is available, a wizard also can research a spell there. To do so, be sure to leave a spell preparation slot open before studying. You can then prepare new spells from written sources you find, making Arcana checks, as necessary. You don’t have to copy them to your spellbook right away, as long as you do so before your next long rest.