I find that depression tends to occur when you feel a need, but also feel that you have no way to satisfy it.
(Sidenote: anxiety tends to occur when there may be a way to satisfy it, but uncertainty is involved and constant diligence is required to reduce the probability of the plan falling apart).
I often find myself confused by the fact that depression is a state that the modern human brain can even feel, despite evolution's relentless drive to purge all traits which are not adaptive to survival and reproduction. Being depressed makes you useless and unattractive, to yourself and others. You do not build, you do not connect, you do not solve problems. How is it that a state of mind which has you feeling and acting the opposite of alive managed to survive a selection mechanism which is designed to ruthlessly eliminate all traits which are not helpful for doing so?
I think the answer is that up until recently, it wouldn't have. Imagine that an animal is in a situation where they truly cannot fulfill their need for food, water, shelter, or reproduction - either because they are truly stuck, or cognitively incapable of finding the right course of action. They would have died quickly.
It is a relatively modern social development which has created myriad complex needs beyond those for basic survival - i.e. needs that are not truly needs, and will not kill us or prevent us from reproducing if they aren't fulfilled. (Examples include a smokingly attractive partner, more instagram followers, a promotion at work, etc).
Along with those myriad complex needs also come an infinitely complex space of possible beliefs about their feasibility. We've gone from problems like "do I have a line of sight to escape this large predator" to problems like "how do I get more likes on Instagram". I don't know that our current emotional machinery was ever evolved to handle problems like the latter particularly well.
Imagine the space of possible outcomes when you smash the system of human emotion and cognition, evolved to ensure basic survival in our caveman days, against the infinite and growing complexity of the modern world, which such a system was not at all designed for. The space of that collision contains everything from the brilliance that put humans in space to the hell of genocides and mass shootings. We humans are literally capable of anything.
It is not surprising, then, that the space of that collision also contains an infinite capacity to direct hatred, shame, and pain towards the self, even to the point of ending it. It is in this space that depression lives and continuously finds new forms.