Los Angeles Superior Court Judge Elden S. Fox ordered Lohan to remain at the Betty Ford Center until Jan. 3 and report back to court in late February.
"You are an addict, I hope you understand that," Fox told Lohan.
A prosecutor had advocated a six-month jail sentence for the "Mean Girls" star, but Fox opted for rehab after reviewing filings by probation and rehab officials and a letter written by Lohan.
The actress cried after hearing the sentence.
The judge acknowledged that Lohan, 24, has been in-and-out of court and rehab since a pair of high-profile arrests in 2007. He told the actress that he would not be manipulated by her, but was giving her a final chance to stay clean.
"You're staying past the New Year's - there's a reason for that," Fox said.
Fox's sentence means Lohan will not be returning to a suburban Los Angeles women's jail for a fourth time.
The judge had threatened to send Lohan to jail for 30 days for each drug test she skipped or failed. He ordered her held for nearly a month during a hearing in September, but another judge overturned his ruling and Lohan has been free on bail and in inpatient rehab voluntarily since then.
Lohan is due back in court on February 25.
Prosecutor Danette Meyers, who has handled the case since its filing, told Fox that Lohan had entered rehab for a fifth time last month only because she was afraid of going back to jail.
"The only reason she's in Betty Ford is because the court was going to send her to jail," Meyers said. "She didn't have some epiphany."
Lohan's attorney, Shawn Chapman Holley, said her client was doing her best and that relapse was part of addiction recovery.
"I would ask the court to not punish her," Holley said. "She's trying her best. She's remaining in Betty Ford."
Fox read Lohan's letter, then laid out the dire consequences of failure. He said Lohan was risking destroying what remains of her already damaged career.
The fate of Lohan's next starring role remained unclear Friday morning. She has been slated to star in a biopic of porn actress Linda Lovelace, but the project has been repeatedly delayed due to Lohan's jail and rehab stints since July.
Friday's hearing was Lohan's fifth court appearance since May, when she ran into trouble with her probation for missing several weekly counseling sessions. She later spent 14 days in jail and 23 days in rehab as punishment.
Her legal woes are the result of drug and drunken driving charges being filed in 2007 after a pair of high-profile arrests. Lohan pleaded no contest to two counts of driving with a blood-alcohol level above 0.08 percent and one count of reckless driving. She was sentenced to three years of probation, but that was later extended after the actress didn't complete the terms of her sentence in time.
Fox allowed her to be released from rehab early based on the recommendations of her doctors and laid out a path for her to be taken off probation by the end of the year. His regimen included frequent counseling sessions, meetings with probation officials and random drug screenings. It also included several incentives, including an assurance that if the actress complied with the terms for 67 days, her probation oversight would end and she would be free to move from Los Angeles.
Within weeks of her release from rehab, Lohan failed a drug test. She acknowledged the result in postings on Twitter and said she was struggling with addiction.