Lets look at two not gates set up like this (called a NOT latch):

Lets assume that the Gate with input A is 1. Forget the other one for now. Suppose A = 1, then the output of the top gate is 0 ("NOT A"). This serves as an input for the second gate. Since the input to this gate is a zero, its output will be 1. The two outputs, as you can see are complements of each other. We don't have to worry about a situation where both gates are given the same input at the same time - its almost impossible for the gate delays to be exactly the same. In the extremely rare case that this should happen, a tiny disturbance in the circuit will stabilize it.
Now the NOT latch as such isn't very useful. Lets add an AND gate before the inputs to each NOT gate like so:

Which is a NAND Latch.

Lets look at the four input combinations:
CODE
AB Q Q'
00 * *
01 1 0
10 0 1
11 Q Q'
Forget the first one for now. In the second combination, A = 0 and B = 1. If you look at the truth table of a NAND gate, its output is always 1 if at least one of its inputs is 0. Therefore, irrespective of the second input at "Gate A", its output will be 1. Hence Q is 1. Now look at the inputs to "Gate B". Both are 1 and hence its output will be a zero. This shows Q' = 0.
We could have started with gate B as well. Since B = 1, Gate B's output is unpredictable if we do not know ehat its second input will be. However, since A is 0, the output of Gate A will always be 1, giving and input of 11 to Gate B.
Combination 3 can be explained similarly.
In combination 4, since both gates are fed with an input of 1, they're outputs will depend on what they're second inputs are. Assume that the latch was SET before we applied the input. The inputs to the top gate (Gate A) are 1 and 1. Hence its output will be unchanged at 0. The same holds for gate B. The inputs to B are 1 and 0. Hence its output is 1.
The same logic applies when the latch is initially RESET.
Now for combination 1 - the one to be avoided. If A = 0 and supposed its second input is 0, Gate A will give an output of 1. Now since the inputs to gate B are 1 and zero, its output is also a one. Now we don't want a latch where both its outputs are the same.
As a convention, the A input is denoted by S' since it resets the latch when it is set to 0. B is denoted by R since it sets the latch. The inputs are active low in this case.
Hope this explained how latches work

.
A flip-flop is technically, a clocked latch i.e. a latch with a clocked input, you use a NAND gate for this purpose:
_Flip-flop_Diagram.svg/350px-SR_(Clocked)_Flip-flop_Diagram.svg.png)
The NAND gates allows your Inputs to "reach" the NAND Latch only when the enable or clock input is held at 1.
Hope this helps
This post has been edited by Louisda16th: 13 Feb, 2008 - 05:13 AM