When all the boundaries have been decided and there are no more disputed territories, both players pass, the game ends, and scoring commences. We like to use the Japanese scoring system. There are a few others, but they all usually end up with the same results at the end of a game.
Before everything, the stones left over that are considered dead are removed from the board and become captures.
Each capture is one point and each empty intersection surrounded by your stones is one point (when we play in person with real boards, we count captures as negative one point for the opponent and put them inside his or her territory to make it easier to score). Whoever has the most points wins.
In Japanese scoring, empty intersections are key to making points, so this is why you do not want to play inside your own territory unless it is absolutely necessary. You also don’t want to play inside your opponents territory unless they have to respond, otherwise you’re giving them free captures in the form of dead stones. Below is a full game I played online with the scoring at the end.