The subnet mask splits the IP into two sections, a network (or subnet) part and a host part. The preceding comments illustrate that nicely.
Anything where the network part of the IP is the same can communicate without having to go "through" a router. The host part of the IP must be different for each device, of course.
If you had 4 computers with these IP/subnet masks set:
host_11 - 192.168.2.1/255.255.255.0
host_12 - 192.168.2.2/255.255.255.0
gateway - 192.168.2.3/255.255.255.0
host_21 - 192.168.3.5/255.255.255.0
only the first 3 could communicate. The fourth would not respond, because it is not on the same subnet.
"Default gateway" is a fancy name for a router, and it needs to be on the same network. You might have seen ipconfig output where there is no default gateway. That means no routing; i.e. communication can only happen between IP's on that network.
Keeping with our example:
host_11 - 192.168.2.1/255.255.255.0; default gateway 192.168.2.3
host_12 - 192.168.2.2/255.255.255.0; default gateway 192.168.2.3
gateway - 192.168.2.3/255.255.255.0
host_21 - 192.168.3.5/255.255.255.0; default gateway 192.168.2.3
If 192.168.2.1 wanted to talk to say, 192.168.3.5, it would end up that 192.168.2.3 picks up that traffic, and then forwards it. (Routing is forwarding.) 192.168.2.3 would need a second IP that is set to something like 192.168.3.1/255.255.255.0. Then, 192.168.3.5 would need to have it's default gateway set to something on it's subnet, which 192.168.3.1 is.
So, in all actuality, it really looks like this:
host_11 - 192.168.2.1/255.255.255.0; default gateway 192.168.2.3
host_12 - 192.168.2.2/255.255.255.0; default gateway 192.168.2.3
gateway - first IP 192.168.2.3/255.255.255.0, second IP 192.168.3.1/255.255.255.0
host_21 - 192.168.3.5/255.255.255.0; default gateway 192.168.3.1
So now the 192.168.2.X and 192.168.3.X networks can speak to each other. Of course, they can't speak to any other network, or the Internet. The gateway would need a third IP connected to an ISP, and have that set as it's default gateway. The gateway then picks up non-same-network traffic from 192.168.2.X and 192.168.3.X.
You can set routing rules for situations where the gateway can't find out everything on it's own. In this example we don't really need to since all networks are connected to the router. However, in situations where you have a network with multiple routers (default gateways), or "networks behind networks", then routing rules have to be specified. That's getting into some advanced stuff. I hope this was helpful.
4
EA over at SF has answered this much better than anyone else I've ever seen. http://serverfault.com/questions/49765/how-does-subnetting-work
– MDMarra – 2010-09-10T18:22:25.080