During the first two years of his incarceration, Andy works in the prison laundry. He attracts attention from "the Sisters", a group of prisoners who sexually assault other prisoners, and their leader Bogs. Though he persistently resists, Andy is beaten and raped on a regular basis. Later, he overhears Hadley complain about having to pay taxes on a forthcoming inheritance. After explaining a legal loophole to Hadley, Andy is reassigned to assist the prison librarian, elderly inmate Brooks, a pretext to allow Andy to work on financial requests full time. Andy's financial advice is soon sought by other guards at Shawshank and by visiting guards from nearby prisons. Hadley delivers a crippling beating to Bogs, after his gang's brutal assault puts Andy in the infirmary. Andy is left alone by the gang from then on.
Andy helps to expand the prison library by writing weekly letters to the state government for funds. When one donation to the library provides him with a recording of The Marriage of Figaro, he plays an excerpt over the public address system, well aware he will receive solitary confinement for doing so. Warden Norton develops a scheme that uses prison labor for public works, undercutting the cost of skilled labor and receiving kickbacks. Norton has Andy launder the money under the false identity of "Randall Stevens", in exchange for allowing Andy to keep his private cell and to continue maintaining the library. Brooks is freed on parole and moves into a halfway house. Unable to adjust to the outside world, he hangs himself. Andy dedicates the expanded library to him. In 1965, Tommy joins Andy and Red's circle of friends, and Andy assists him in getting his GED. When he hears the details of Andy's case, Tommy reveals that an inmate at another prison, Elmo Blatch, claimed to have committed a nearly identical murder, suggesting Andy's innocence. Norton, fearing Andy might tell of his corruption if released, refuses to cooperate. After they argue, he throws Andy into solitary confinement for two months. Norton has Hadley kill Tommy, claiming he was attempting an escape. Andy returns to his regular cell block and tells Red of his dream of living in Zihuatanejo, a Mexican Pacific coastal town, and setting up a hotel with boat rides for his customers. While Red shrugs it off as unrealistic, Andy instructs him, should he ever be freed, to visit a specific hayfield near Buxton to retrieve a package.
The next day at roll call, Andy's cell is empty. When Norton, angry at Andy's disappearance, throws one of Andy's rocks at the poster of Raquel Welch, the rock tears through the poster, revealing a tunnel that Andy has dug with the rock hammer over the last two decades. The night before, Andy switches Norton's ledger with his prison-issue Bible. Taking the ledger, his chess set, and one of the warden's suits, he escapes through the tunnel and a narrow sewage drain during a thunderstorm. After escaping, Andy poses as Randall Stevens to withdraw most of the corruption money from several banks, then sends evidence of Norton's corruption and murder of Tommy to a local newspaper. The police arrive at the prison, and Hadley is arrested, but Norton commits suicide to evade arrest.
Red receives parole after serving 40 years and is allocated the apartment where Brooks committed suicide, and works at the same grocery store. Red begins to feel fear of the outside world, just like Brooks. Red remembers Andy's advice and visits Buxton. There, he finds a cache of money and a note left by Andy, telling him to get to Zihuatanejo. Red violates his parole and travels to Fort Hancock, Texas to skip the border to Mexico. The two are happily reunited on the beach to begin a new life.