Go to this XDA thread and download the newest firmware for your region. Download Odin for Windows (search for "Heimdall" if you are on Linux). Then do the following:
- Put your phone into Download mode
- Remove the battery
- Hold VolumeDown plus the Home key
- Reinsert the battery.
- Let go of the power button only about 1 second after the white device logo appears (if it does)
- After a few seconds a yellow triangle with an android in it should appear, you can let go of the keys since this is download mode.
- Start up Odin.
- Attach the phone to USB so that Odin recognizes it. If it doesn't, reboot your phone, exit Odin, and go back to step (1).
- Load the .zip file you downloaded (the custom ROM) into the PDA field of Odin.
- Load the 512 .pit file into the PIT field of Odin.
- Check "Re-partition".
- Hit Start. Odin will tell you when it's done; if it fails, reboot your phone and try again. (This happens occasionally).
Note that if you need to back anything up, you should do that beforehand and save the backup to your PC, since everything (except probably the external SD card) will be wiped on the phone. Also note that you do this at your own risk; Odin is very powerful and misusing it can damage your phone. Re-partition in particular can be troublesome if you select the wrong PIT file. I've used Odin dozens of times myself safely, though.