Hi,
A comparator does exactly that...it compares two voltages and makes the
output go either high or low depending on what those two voltages were.
Calling the non inverting input V+ and the inverting input V-,
when V+ goes higher than V- the output goes high, and when V+ goes lower
than V- the output goes low. You can design lots of things just knowing
this.
There is a little bit more to it however, when you start to think about what
voltages the inputs can handle. Sometimes the input can handle voltages
near or even at ground (0v) and sometimes they cant. Similarly, sometimes
the inputs can handle voltages near Vcc, and sometimes they cant. These
usually depend on the specific device being used, ie the part number.
There are even some comparators that can handle a tiny bit negative on
their inputs even though they dont have a negative supply voltage.
Take the LM339 for example. The inputs you can use range from 0v (ground)
up to Vcc minus 1.5 volts. This means you can use 0v but you can not use
Vcc on the inputs if you expect the device to work as a comparator.
To answer your question about 0v and negative voltages, you can use
'negative' voltages (relative to ground) when you also use a negative supply
to power the device. In this case you have a positive supply voltage (like
+5v) and a negative supply voltage (like -5v) and now you call ground 0v,
which is between the two.
In the case where you only use a positive supply voltage (like +5v) and you
ground the minus power input to the device ground is still 0v but now it is not
possible to use a negative input because the min spec for the LM339 is 0v.
When we use the negative supply (such as -5v) then we can use a voltage
as low as -5v, but without a negative supply we can only go down to 0v.
To recap LM339 operation:
With a +5 and -5 power supply (0v=ground) we can use any voltage from
-5v up to +3.5v on the input for correct operation.
With a single +5v power supply (0v=ground) we can use any voltage from
0v up to +3.5v on the input for correct operation.
One other thing that has already been mentioned by others here is that for
the LM339 we have to connect a resistor from the output pin to the positive
supply voltage to get the device to work correctly. This value can be around
1k to 10k for decent operation.
When the output goes "Low", this means it goes close to the negative supply
voltage (-5v) or with no negative supply it goes down close to 0v.
When the output goes "High" this means the output goes to a rather high
resistance, which gets 'pulled' up by the output resistor talked about above
(1k to 10k roughly), and this means it goes just about up to the positive
supply voltage (+5v) with no load. With a load the output will not reach +5v
but will be somewhat lower depending on what the load resistance is. For
example, with a 10k pullup and a 10k load to ground the output will only go up
to +2.5v and that's it, but sometimes the output load goes to +5v and so
sometimes we dont need a pullup resistor. A small relay coil wired from the
output to +5v would still function normally because it does not need a pullup
resistor. On the other hand, a TTL gate load would need a pullup resistor to
get the switching time within reason.