A subnet mask defines the range of IP addresses that can be used within a network or subnet. It also separates an IP address into two parts: network bits and host bits.

Subnet masks are used when subnetting, which is when you break a network up into smaller networks. By adjusting the subnet mask, you can set the number of available IP addresses within a network.

For example, a common subnet mask for simple home networks is 255.255.255.0. This subnet mask allows up to 254 usable IP addresses within the home network. In other words, up to 254 computers, phones, and other internet connected devices can connect to your router/network and access the internet.

A simple network / subnet. Source: What Is My IP Address?

Subnet masks break an IP address up into network bits and host bits. When a device sees the network and host bits of another device's IP address, it can figure out if the other device is part of the same network (home, business, etc.), or is somewhere else online

An IP address and subnet mask. Source: IPv4

Check out this article for more information about subnets, subnet masks, and how they work.