Let's say I am a Beast Master ranger and have a baboon animal companion. Is there a spell or magic item that would let it know how to read? Write?
A Helm of Comprehending Languages will work
The Helm of Comprehending Languages states:
While wearing this helm, you can use an action to cast the comprehend languages spell from it at will.
The comprehend languages spell in turn states:
For the duration, you understand the literal meaning of any spoken language that you hear. You also understand any written language that you see, but you must be touching the surface on which the words are written. It takes about 1 minute to read one page of text [...]
Thus we could have a creature understand any spoken language and read and written language.
Even if a creature could not use this helm accurately, there is also the awaken spell which states:
[...] The target must have either no Intelligence score or an Intelligence of 3 or less. The target gains an Intelligence of 10. The target also gains the ability to speak one language you know [...]
Thus we could have a beast or plant learn to speak a single language the caster knows (presumably they also can understand that language as otherwise it's a rather useless feature, but perhaps they cannot; what it means to "speak a language" is very very slightly ambiguous). Also this spell would give a creature intelligence enough to use the Helm of Comprehend Languages if they previously could not.
One potential way to have this work on any beast/plant regardless of intelligence would be to use the feeblemind spell which states:
[...] On a failed save, the creature's Intelligence and Charisma scores become 1 [...]
This would thus allow them to be a valid target for awaken.
Using a Helm of Comprehending Languages and the awaken spell (and perhaps feeblemind) we could have a plant or beast read and understand the speech of any language as well as speak a language the caster of awaken knows. Unfortunately, I've found no way of teaching an animal companion how to write.