Well, you first have to have permission to work in Spain. If you are an EU citizen then you have the right to live and work in Spain. Others will have to go through one of the visa processes.
Afterwards, you may want to register as an autónomo (freelancers). This has both advantages and disadvantages so it will depend a little bit more on your business model and what is right for you. Note: you can invoice up to a certain amount per year without being a freelancer. The amount fluctuates every year so it is a good idea to take a look at the current figures to see how much it is.
If you decide to become a freelancers, here is a guide on How to become an autónomo (freelancer) in Spain.