Does your button pin have a pull-up or pull-down resistor to keep it from floating when the button is open? It needs one or the other.
It looks like your sketch is expecting a pull-up resistor. If you didn't add an external one, change:
pinMode(buttonPin, INPUT);
to
pinMode(buttonPin, INPUT_PULLUP);