What to do before root and flash my phone? Right now I have in my phone the stock rom and I haven't rooted it. I want to root it and change the rom. 


What should I do first, root or flash the rom?
I don't want to lose all the configuration, apps, apps data or files (images, music, videos, etc in the SD). How can I do to be sure to avoid this?
I want to make sure I can go back to the stock rom if I need, is there any special thing I should do?


If its relevant, I have a Samsung Galaxy I5510.

Thanks!
You should flash the ROM first, unless you need root in order to flash it.  (You can flash via Odin without root, but you need ROM Manager, which needs root, or something to flash if you don't use Odin).  When you flash a ROM that's rooted, your device will be rooted; if the ROM is not rooted, the device will not be rooted.
You need root to make a full backup.  See How to backup an Android device?
If you root first, you can install a custom recovery (e.g. ClockworkMOD, via ROM Manager) that will let you create a Nandroid backup of the full ROM.  That said, with a Samsung device you don't need to do this.  You can use a tool called Odin to reflash the original firmware as long as you've found it for download somewhere.


If you need more details on anything I've mentioned, I suggest searching first -- we have lots of questions on rooting, backups, Nandroid / Clockwork / ROM Manager, and Odin.  See also How do I root my Android device?, which has some info on considerations and things you can do before rooting.