Why use resistors?

Hi,
Why do buttons need resistors?
Before I knew this fact, I used to wire one leg to ground and the other to digital, and they worked perfectly.
Only recently I found out that one leg goes to 5v and the other goes to ground, a digital pin, along with a resistor.
Why is this necessary?
I've read a lot of explanations but I can't understand any of them.
Is is safe to connect one to ground and the other to digital?

Thanks!

It is called a pull-up. When you button is open, there is no defined voltage on your input pin. It is just freely floating with EM noise or anything. A resistor to 5V ties it to that potential.

As long as you use internal pull up in your setup you are OK. IF YOU DRIVE THE PIN hIGH as output and connect it to GND it may work but you may be damaging the IC - it depends on IC, supply voltage, buton on resistance, ...

That they worked before was just luck. When your digital pin isn't connected to ground, what makes you think it must therefore be high?

As mentioned, they are subject to all sorts of noise, so could easily give you a false low. The pullup guarantees it's high when you don't switch it low.

The thing some people don't get the hang of is that CMOS inputs sense voltage without taking
any current (well far far less than you can measure without specialist lab equipment *). They
sense voltage, so a pin not connected to a defined voltage is sensing noise, capacitive coupling
from nearby objects, ions in the air, almost anything but the state of the switch!!

(*) measured in picoamps, ie 0.000000001 mA, that sort of current!