Aditya Singh, 36, was asked on Saturday by the airline staff to produce his identification but he failed.
He had a badge which apparently belonged to an operations manager who lost it in October last year.
Singh claimed to have found it in the airport.
According to the police, Singh arrived at O'Hare International Airport on 19 October from Los Angeles.
"According to Assistant State Attorney Kathleen Hagerty, Singh was "scared to go home due to Covid," reports BBC via Chicago Tribune.
He managed to live on handouts from other passengers, she told the judge in the case.
Cook County Judge Susana Ortiz expressed surprise at the circumstances of the case.
"So if I understand you correctly, you're telling me that an unauthorised, non-employee individual was allegedly living within a secure part of the O'Hare airport terminal from 19 October 2020 to 16 January 2021, and was not detected?
I want to understand you correctly," she asked the prosecutor who outlined the allegations on Sunday.
Singh lives in a suburb of Los Angeles and does not have a criminal background, according to Assistant Public Defender Courtney Smallwood.
It is not clear why he was in Chicago.
He has been charged with felony criminal trespass to a restricted area of an airport and misdemeanour theft.
He has also been barred from entering the airport if he is able to post the $1,000 (£738) for bail.
"The court finds these facts and circumstances quite shocking for the alleged period of time that this occurred," Judge Ortiz said.
"Based on the need for airports to be absolutely secure so that people feel safe to travel, I do find those alleged actions do make him a danger to the community."
The Chicago Department of Aviation, which oversees the city's airports, said in a statement: "While this incident remains under investigation, we have been able to determine that this gentleman did not pose a security risk to the airport or to the travelling public."