system
Given the following post, respond as if you're commenting on Reddit:
human
ELI5: What are subnets and network masks and what is their purpose?
gpt
Let's start from the basic thing as a cable. A cable where a few computers are connected to, together. Imagine that they all can hear each other via the cable directly, much like people who are in the same room. Computer network cards talk on that level.

At some point the rooms grew up to become so large that there was almost no quiet time left, and some people would have no time left to say all they had. At that point engineers invented a peer-to-peer discussions where people would talk only to another one person at a time and the room would magically make it so that only the peers can hear them talking. However, everyone would still need to shout "who is John?" if they have something to say to John and they never spoke to John before. John would hear that and reply directly. You can imagine that as the rooms grew to accept more people to "the network", the room became noisy again just from those shouts out.

At that point the engineers invented that there could be more than one room and that way they can keep the rooms semi-quiet. And the rooms would be connected by a corridor with a door - a gateway - in each room, with a gatekeeper who can pass your words to another room and their replies back. But there is a problem - people shouting names in one room cannot be heard in other rooms, therefore they won't receive an answer if they simply shot a name. They don't actually need to hear it, all they need to know is that they need to pass the message via the gatekeeper.

This is where we go back to our wire with the network card. The engineers were not allowed to change how the hardware worked, therefore they created a whole new level on top of the existing one. Let's name it the Internet Protocol (IP), also known as layer 3. All computer systems would start using the IP addresses, and each device would just know a way to convert layer 3 addresses to layer 2 addresses if they would be connected to the same cable.

Now going back to our rooms. While people's names are random (except they have to be unique, and they in fact are across all computers), we cannot say that everyone whose name starts with A is in room A. Everyone can be paced in any room. However the IP addresses were designed so that every address of a given room conforms to a certain rule, and every non conformant address is definitely outside this room. Those rules are the network masks from your question and the rooms are the subnets.

Now to say something to another person you need to take your IP address, apply the network mask on it, then do the same with the destination IP address and if the results are the same - the other person is in the same room and you can start talking directly. If the results are different then your need to pass your message to the gateway.