Wikipedia has a nice list with links to several sources.
Both of your narratives are fairly well represented. There is a large chunk (anecdotally, I'd say about a third) of people who have lost a job or suffered a major financial catastrophe (medical, accident, etc.) and as a result ended up unable to afford housing. Cheaper housing would reduce the numbers of these people (see large concentrations of the homeless in cities with high housing costs, like NYC, LA and Seattle), as would more/better jobs. Another factor is social support network: To become homeless you have to not only lose your house, but also not have any friends or family willing to let you stay with them while you get back on your feet.
Substance abuse is quite common as well. Not every homeless person is an addict, but many addicts end up homeless, and they make up a big chunk (confirmed by statistics). Unlike the previous category, these have an additional barrier to rehabilitation: Because of the drugs it is difficult to get a job and keep it, and even if they did, it is hard to save money and move into a place because the addiction tends to take priority over that. These people wouldn't necessarily be helped by cheaper housing and more jobs, you would need to also treat the addiction for most of them.
Mental illness is common also. It's worth noting that drug use and mental illness can be co-morbid, and also homelessness itself (or the stress leading up to it) can cause mental illness, but there are many examples of people becoming mentally ill which caused homelessness. Two common ways are becoming unable to work, or being financially ruined by costs of treatment. In any case, it's hard enough for a healthy homeless person, with severe mental illness it becomes even worse. And often the homelessness also makes the illness worse.
Another large group are the veterans. To be sure, mental illness and/or substance abuse can play a role here, but also there is an element of difficulty in adjusting to civilian life (coupled with poor support network, lack of suitable jobs).
I think yet another factor is that while there are often many resources available for the homeless, they are not always aware of them or don't take advantage. People who are forced to live on the streets for years are naturally not the best informed.
Some people like to claim that all homeless have only themselves to blame. There is certainly no shortage of people who brought it on themselves through their actions. But that hardly accounts for all of them. Generally, in life you sometimes experience sudden financial shocks - such as loss of income - and most people have a variety of resources to get through these (like friends and family, savings, good credit, being employable). But if you the shock is large enough to overwhelm your contingencies, or if you get caught at a time when your contingencies are unavailable, you can end up homeless easily enough. Once you're in that position, a lot of practical difficulties arise and prevent you from getting out:
- You can't maintain hygiene or groom which leads to poor impressions on potential employers
- You have limited access to the internet so looking for jobs is hard
- You have no address so many places are hard to deal with (banks, government, etc.)
- Your health deteriorates, you live in unsafe environments where you get victimized by criminals and other homeless
- You are constantly around substance abusers and at high risk of abusing yourself
- Your mental health suffers, and obviously this sort of life is not the best for making you feel positive and motivated to go find a job and get back on your feet
So it is usually a case where a perfect storm of misfortunes traps the person in this state, and it is very hard for them to escape it.