Help understanding how territory is assigned

Hi - I’m a beginner Go player trying to wrap my head around how territory gets decided. Below is a game I played against an AI - the AI (Bobby) passed, then I passed, and then the game ended with this territory calculation:

I don’t quite understand why the territory along the edge, as well as the black stones, were treated as captured and in white territory. Can someone help me understand, as well as whether this loss was inevitable at this point or whether I could have continued playing in a way that would have secured that position?

Those stones
were treated as captured and in white territory
because there was no way for Black to prevent White from capturing them. ​ ​ ​ is very relevant here.


Thank you; this makes sense.

Thats bit complicated position for a beginner and a bit difficult to show here why all the black stones under the white on the left are dead but let say.
To capture white you will need to play in the space in upper right
But to play there it can only be the last moves after taking away all the liberties around the white group.
Problem is that you cannot do this, as you will always get a surrounded group with only 1 liberty , group that white can capture.
By capturing you white will build eyes. Eyes are empty surrounded spaces like the one in the upper right.
2 eyes are enough to live, to make your stones safe from capture. In fact here whire has already 2 eyes and black has no eye.