Can you pair both a Mac and an iPad 2 with the same Apple wireless keyboard, but not at the same time? Or do you have to un-pair one, and then pair it with the other?
Yes you can.
First, make sure Bluetooth is turned off on your iPad. And enable it on your Mac (make sure the box that says "discoverable" is checked). Then turn on your keyboard and follow the steps to pair it using the Keyboard System Preference pane. Once the keyboard is paired, uncheck "discoverable" (which should remain off unless you wish to pair another device) and then turn off Bluetooth.
Next, head over to your iPad and enable bluetooth. Turn your keyboard off (hold the power button till the light glows and then dims). Turning it on again will get the iPad to pick it up and a prompt will ask you to enter a 4-digit sequence of numbers and the "ENTER" key. Follow the steps. Once that is done, it will be paired.
To use the keyboard on the iPad, make sure the device has bluetooth enabled and that your Mac has that service disabled (you should disable the service before trying to connect the keyboard to the other device).
I just tested it using my Mac and my iPhone 4 and it works perfectly. Sometimes the device takes a second attempt to locate the keyboard, but it does indeed work.
For those who are looking to do follow the accepted solution with the Apple Magic Keyboard: it is not possible. As @bmike points out, the firmware of the keyboards remembers the last paired device: Can you pair macOS and iOS device with same Magic Keyboard and switch between them?
I had the same problem; I also did not know how to make the BT device undiscoverable. What I did was removed my keyboard device from my iMac (Go to System Preference and then to Bluetooth) by clicking the cross icon when you select the device. I then turn on BT on my iPad, waited a while and the two got connected without a problem. Hope this helps.