From the windows of a seven-story building on East 151st Street in the Bronx, Manhattan rises like Oz in the distance, a glittering reminder of why so many people want to live in New York City.

Inside the building, children sense something is wrong. Their parents are bogged down by backpacks, suitcases, strollers and worries — the burdens carried by many people, unemployed and underemployed, who cannot afford to live in New York.

New York City must, by court order, provide temporary shelter to any eligible person, and to comply, the city spends about $1.8 billion a year on shelters, apartments, hotel rooms and programs.

It is a vital service for people in need, and it is a costly one. The city does not make it easy to qualify for shelter, and the housing market does not make it easy to emerge from the system. The arduous process can stretch over more than a year and has many phases — the stages of homelessness.

They begin in the building on East 151st Street, the city intake center known as P.A.T.H., or Prevention Assistance and Temporary Housing, with the application and the gauntlet of interviews. Then, temporary placement in shelter for up to 10 days while the city determines whether an applicant is indeed homeless. Next, placement in a long-term shelter.

Families stayed in shelter for an average 414 days, according the mayor’s annual management report for fiscal 2017.

Moving out is a struggle in itself. Applications for rental assistance. The search for an affordable apartment. And for the fortunate ones, the move.

The city’s surge in homelessness can be traced to 2011, when the state cut funding to a key rental assistance program. By 2012, the overall homeless population had jumped by 11 percent to about 57,000 people, and by 2013, the number was about 64,000, according to an annual count overseen by the Department of Housing and Urban Development.

Since Mayor Bill de Blasio took office in 2014, the number has continued to creep up to a current estimate of 77,000 people. The record number has come even as the city has diverted tens of thousands of people from homelessness by pouring millions of dollars into new rental assistance programs and legal assistance to fight evictions. The programs cannot keep pace with runaway rents, stagnant wages and vanishing affordable housing. About 100 families go to P.A.T.H. each day.

In hopes of ending the use of private apartments and hotel rooms as stopgaps, the Department of Homeless Services is expanding its shelter system under a plan to open 90 facilities over five years.

The New York Times looked at homelessness step by step through the eyes of several families, over the final months of last year.

The desperation and embarrassment of having nowhere else to turn and the daily frustration of living with little privacy and curfews were immeasurable. The joy of families moving into their own homes was palpable.