The subnet mask is used to determine where the network number in an IP address ends and the node number in an IP address begins. A node is anything on a network that needs an IP address to communicate (a PC, server, router, etc).
With the above answer given I can imagine that this does not explain it quite enough. It's also much more complicated and yet also very simple. There are lots of sites that will give more detailed information though, but here is some right now:
IP addresses are grouped by something called subnet mask. Every ip address has a corresponding subnet mask. The subnet mask specifies the range of the ip addresses in a group. The subnet mask looks a lot like an ip address. It is made up of four eight bit numbers separated by periods. These numbers once again range from 0 to 255. A typical subnet mask is 255.255.255.0.
There are a couple intresting things about subnet masks. They don't really behave like you would initially expect them to. The numbers of a subnet mask count ip addresses that are not there. This means higher the numbers of a subnet mask are the less ip addresses belong to it.
255.255.255.255 = There are no ip addresses in this range.
0.0.0.0 = This is the range of all ip addresses.
Lets take one of the most basic subnets the 255.255.255.0 one, and see how many addresses are in it's range. The first step is to put the subnet into binary. Let go ahead and do that now. If you don't know how to put something into binary read Binary Numbers for more information.
So how do we find out how many ip addresses are in this group? Well its rather simple actually. Just count the number of zeros, and then take 2 to the number of zeros power. In this case it would be 2^8 = 256. Another way to do it is to multiply 2 times itself 7 times. 2*2*2*2*2*2*2*2=256
So we have two hundred and fifty six ip addresses in that range! Another thing that is intresting to know. Subnets will always be all ones on one side, and all zeros on the other. I mean that they will always look like 111111000000 and never like 1010101101.
Lets take a closer look at what a group of ip addresses looks like. Using the 255.255.255.0 subnet from above let me make a table.
Every group of ip addresses, has a Subnet Address, Broadcast Address, and Gateway. Both the Subnet Address ip address and the Broadcast Address ip address are used to send information to every ip address in the group. The Gateway acts sort of like the group's controller. For instance, let's say that your computer is on the ip address 192.168.1.3. When you send send information to the internet, your computer sends data to the gateway. Then the gateway sends that data on to the internet. The same thing is true when you get data from the internet. The internet sends data to the gateway, and then the gateway passes that information on to your computer. The gateway can be on any ip address in the range. Usually it is on the second ip address in the range, or the second ip address from the end of the range. The Subnet Address is always on the first ip address in the range, and the Broadcast Address is always on the last one in the range.
Okay I'm going to change our subnet from 255.255.255.0 to 255.255.255.240. Lets say the ip address of our computer is 192.168.1.132. How many ip addresses do we have? Well lets convert the subnet to binary. You should get the following binary subnet.
Okay, we have four zeros. So take 2^4 which equals 16. Alternatively 2*2*2*2 equals 16 as well. We have 16 ip addresses in our range. Well lets draw out our table. We can't draw our ip address table without knowing where the range starts. This is how you figure that out. We take our subnet and AND it to our ip address converted into binary. Go ahead and conver our ip address into binary. You should get the following.
So look at the first column. We have a 1 and a 1. 1(true) and 1(true) = 1(true) The next column is the same thing. The thrid column is a 0 and a 1. 0(false) and 1(true) = 0(false). Continue doing that for the whole number. Now that we have our result lets take it and convert it back to base 10, so we can get the first ip address in our range.