Fan Pinout Defined
2- and 3-pin fan speeds can only be changed by altering the voltage supplied to them. On both, the Black wire is Ground, and the Red wire is the + DC voltage supply. All fans in a computer are designed for max 12 VDC supply.
A 2-pin fan generally has on the end of its wires a male 4-pin Molex connector, but only 2 pins are used. That mates with a power supply connector directly from the PSU. Done that way, it always runs at full speed. Instead, if it is plugged into a third-party fan controller, that unit will supply a voltage that may be reduced (for slower fan speed) by changing the manual setting on the controller - usually, a knob that turns. It also might be possible to get an adapter to allow you to connect such a fan to a mobo 3-pin fan port, thus allowing the mobo to control the fan's speed (but see comment later on speed monitoring). 2-pin fans have no way to send out a speed signal, so you cannot measure the speed of such a fan.
A 3-pin fan is generally connected to a mobo fan port and its speed is changed by changing the supply voltage on Pin #2 of the mobo port. The third pin (Pin #3 on the port) is connected usually to a Yellow wire to the fan. The fan motor generates a pulse signal (2 pulses per revolution) which is sent on the Yellow wire back to the mobo for monitoring. The mobo can do 2 things with this. It will count pulses and generate a fan speed measurement for display by software. It also may check this signal for failure - that is, are there speed pulses coming or not? - and issue a failed fan warning if it gets no signal. For this reason, if you are using this fan port for some other fan type (like an adapted 2-pin fan) many mobos allow you to tell the fan port control system to Ignore this port's speed signal, thus eliminating false failure warnings.
A mobo case fan port usually has an automatic fan speed system available and activated by default. It works this way. The mobo has a temperature sensor built into itself somewhere the maker thinks is vital, (like close to the Northbridge chip) and its software setup has some control parameters like a target temperature and a controller gain, etc. pre-set. The automatic control system constantly compares the measured temperature to the target and alters the fan speed in an attempt to keep actual measured temp close to the target. On many mobos, you can alter the target and the fan speed control strategy if you so choose, either in BIOS Setup or using a software monitoring utility that comes with the mobo. You also have options to turn off automatic fan control and use fixed manually-set fan speed. NOTE that the actual fan speed as measured by the pulse signal is NOT used for control - it is only measured so you can see it if you wish. The mobo connector for a 3-pin fan is set up mechanically so that you can only plug the connector in one way - the correct way to get the right signals to the right pins.
A 4-pin fan works similarly, but with an important difference. The fan motor itself is different to work with this system. A mobo 4-pin fan port has the same three pins and signals on Pin #'s 1 to 3. One important difference, though, is that Pin #2 ALWAYS has the full +12 VDC on it. The added Pin #4 carries the PWM (Pulse Width Modulation) signal from mobo to fan. This is like a square-wave signal at about 25 kHz frequency, but it is not just 50% on. Its "% On" value is varied by the mobo control system. It is used as an "inhibitor" in the sense that an "On" state causes current flow to the fan motor to be interrupted so the fan slows down. Inside the fan motor casing there is a small chip that uses this PWM signal to control the flow of current (from the fixed +12 VDC supplied by Pin #2) through the fan motor, thus achieving fan speed control. The advantage of PWM control is that, since the voltage supplied to the motor is the full +12VDC for SOME of the time, it is much easier to start the fan and to keep it running at slow speeds.
The mechanical details of the fan connectors are such that you can plug any 3-pin fan or 4-pin fan into any 3-pin port or any 4-pin port. It's just that the result is not fully what you want if you mismatch them, but there is backwards compatibility. If you plug a 3-pin fan into a 4-pin port, it will receive a constant +12 VDC supply from Pin #2 at all times, and hence it will always run at full speed. It has no way to use the PWM signal, and it does not even receive it with no 4th wire. If you plug a 4-pin fan into a 3-pin port, it WILL run at changing speeds (unlike what Zerk2012 says above) because its +VDC supply on Pin #2 is being changed by the mobo, and it is NOT receiving any PWM signal from Pin #4 because the port does not have one. The only difference is that it is a little less easy to control it at low speeds because it is working exactly like a 3-pin fan. From this you can see that, although backwards compatibility is designed in for partial function, ideally you should connect to any mobo fan port exactly the type of fan it is designed for - 3-pin or 4-pin.
Fans for use on CPU cooling are exactly the same, but the mobo control system for the CPU_FAN port is different in important ways. The first is that the sensor for true measured temperature is built into the CPU chip (by its manufacturer) and NOT in the mobo. And of course, this means that the target temperature and control loop parameters are specific for the CPU chip type installed in the socket. The second has to do with fan speed monitoring. Many mobo's check more carefully the speed on the CPU cooling fan. On such systems, if it ever appears to have failed (no speed signal coming from motor to mobo) the system will send out an alarm signal (often with a big alarm sound) to tell you, then shut down the entire system almost immediately to avoid the possibility of overheating and permanently damaging the CPU chip. Such a system does not even wait for the CPU temperature measurement to go too high. On these systems, if you use some different CPU cooling system (like, the CPU fan is connected to a PSU Molex output and not the mobo port), you need to tell the BIOS NOT to use its automatic CPU cooling system and NOT to monitor that fan. For both of these reasons it is a good idea to use the CPU_FAN mobo port for the CPU cooling system and to allow it to do its automatic control (that is the default setting in BIOS). It is also important NOT to plug any other fan into this port - the port should ONLY be used for the CPU cooler.
On a mobo there normally is one CPU_FAN port (sometimes a secondary one, too, for an additional CPU fan), one or more SYS_FAN ports for case ventilation, and maybe a PWR_FAN port. The SYS_FAN ports are the ones guided by temp sensor(s) in the mobo and used for case fans. A PWR_FAN port is an odd one. It is designed for use with certain PSU units that have coming out of them a set of wires that look just like 3-pin fan wires, and end in a common 3-pin fan connector. The function here is ONLY to allow such a PSU to send to the mobo its internal fan's speed signal so it can be monitored and displayed by software. Such a mobo port does not supply the PSU fan with power, nor does it attempt to control that fan's speed. Some PSU's do have automatic speed control of the fan inside them, but that control is done entirely within the PSU. So if you have a PWR_FAN port on your mobo but no such wires from the PSU, ideally you connect nothing to that mobo port. However, many mobo makers have connected Ground to that port's Pin #1 and + 12VDC to its Pin #2, so that if you plug into it any 3-pin or 4-pin fan, that fan will run at full speed all the time. There is no control of speed, but it works.