The gemara says that oral torah cannot be written, and written torah cannot be recited by heart. It compares one who writes halachos to one who burns a sefer torah. Rashi explains that if a fire would break out on shabbos it would be forbidden to violate shabbos to save the sefer of halachos, therefore the initial writing of it causes it to be burned. Rashi offers a second interpretation that it is destructive to the oral Torah to commit it to writing. The second explanation is difficult, why is it equivalent to burning of a Torah? It seems that the problem with writing oral torah is that it make what should be infinite into something limited and finite. Therefore, by writing halachos one is inherently considering it to be no longer disputable and no longer open for debate. He is "burning" the torah that could have been discussed and introduced on this topic by committing the torah to writing. The exception to this rule the gemara says is when one writes a chiddush that presumably would otherwise be forgotten. The writing of a chiddush is necessary for the preservation of torah and therefore wouldn't be a violation of writing what shouldn't be written.
Similarly, one is not allowed to read the written torah by heart. Tosafos asks from various written sections of the Torah that were accustomed to being recited by heart in the context of davening. Tosafos answers that it is only problematic when one recites a written section by heart with the intent of being motzi someone in their obligation i.e. reading krias shema for someone who is going to be yotzei through sho'meia k'oneh. Tosafos also limits the violation to chumash as opposed to nevi'im and kesuvim.
The Shulchan Aruch (O.C. 49) says that sections which people are familiar with such as shema and birchas kohanim one is allowed to do by heart, but doesn't quote Tosafos who permits anything which is not being done to be motzi others. The rationale of the Shulchan Aruch seems to assume that the problem is that we afraid of one making an error and misquoting from torah she'bichsav, therefore things which are well known are permitted. This is also the implication of the biur halacha citing Tosafos Yeshanim to permit reciting hallel by heart. However, the limitation of Tosafos to when one is being motzi others seems to be that when done casually and not to be motzi someone's obligation, it isn't significant enough to qualify as a prohibition.
The Mishna Berura uses Tosafos as cited by the Gr"a and Radvaz to permit one to read the parsha along with the ba'al koreih even though they don't have a chumash open in front of them. The M.B. also quotes from the chavos ya'ir to permit one to recite tehillim by heart combining the heter of the chavos ya'ir that it qualifies as tefilla and the heter of tosafos that one isn't being motzi others. It is interesting that they don't quote the other distinction of tosafos who limits the prohibition to chumash.