Preformers typically read the script for their scenes multiple times to remember it and practice delivery, then go to rehearsals. In rehearsals, all the actors perform the entire scene together, starting with scripts in their hand. They put the scripts down once they’ve performed the scene enough to perform it from memory.
It is still fairly common for actors to forget lines during scenes, and the script supervisor will prompt them to re-deliver a line. Usually they back up 10 seconds and go for it again, they don’t have to re-perform the entire scene. The final edit of any given scene is usually stitched together from 5+ performances, even if everyone nails it first try, because you have to get coverage and you need to get different camera angles (only one camera is typically in operation). If you watch soap operas you will notice choppy editing because they have to re-take a lot (no time to completely memorize scripts) and don’t have the time to do careful editing, what with delivering an hour of footage every day and all.
Depending on the show, filming may go ep 1/ep 2/ep 3/ep 4, in which case they’re essentially memorizing the entire episode by memorizing each constituent scene, or multiple episodes may be filmed simultaneously. The Office would have been the former case more often than not, but shows that make heavy use of filming on location or have large casts (like Game of Thrones) will film up to 4 episodes simultaneously. If you’re flying the crew out to the desert to film, you want to be efficient and film every single desert scene you’ve written this season over one filming block, rather than fly back and forth and back and forth 10 times a season.
Actors in theater spend much more time memorizing lines on their own and much more time doing rehearsals, because they obviously can’t just do a retake when they mess up, and they’re not doing a full hour of new material every week.
On shows where the performer speaks directly into the camera, like Letterman or Colbert, there is a display called a teleprompter that shows the script on the camera itself so they’re just reading aloud. It would obviously be impossible for Letterman to memorize an hour of dialogue 5 days a week.