tokamak,
The subnet mask is a way of spliting a set of IP addresses into segments or subnets. The easiest scenario is taking a Class C IP address (ie. 3 specified octets X.X.X.?) and spliting it into 2 segments. Each segment would consist of 128 IP addresses. There are two ways to figure out what the subnet mask should be for this configuration (the right way and the easy way). These calculations are for Class C IP addresses and their subnet masks. They may work for Class B, but I have not tried them.
The easy way is to divide the number of segments into 256 to get 128 and subtract that from 256 to get 128. Therefore the subnet mask becomes 255.255.255.128
The right way is to reserve the number of bits within the octet that are needed to split the 256 into 2 segments. In this case, we need 1 bit to represent 2 segments. The left most bits are always used in the octet. Therefore the bit representation is 10000000 or 128 in decimal.
To take another example, suppose you wanted to segment the octet into 8 pieces. Calculating again both ways gives:
Easy:
256/8 = 32
256 - 32 = 224 ----> subnet mask becomes 255.255.255.224
Right:
3 bits are required to represent 8 items therefore last octet is 11100000 or 128 + 64 + 32 = 224 in decimal
subnet mask becomes 255.255.255.224
The next question becomes how is this mask used for something useful
It is used to determine what segment or subnet a particular IP address belongs. Going back to the original subnet mask 255.255.255.128, let's take IP address 192.168.0.111 (this is what the first article minendo posted talks about). If we do a bitwise AND, we get:
11111111 11111111 11111111 10000000
11000000 10100100 00000000 01101111
------------------------------------------------------
11000000 10100100 00000000 00000000 = 192.168.0.0
This represents the network address for the subnet. There are a couple other calculations that can be done, but unfortunately I am running out of time so hopefully someone else can fill them in.