If your application for a residence permit has been rejected, this means that you do not have the right to stay in Sweden. If the Swedish Migration Agency rejects your application while you are in Sweden, you will have a certain amount of time to leave the territories of the Member States and the Schengen area. It is stated in the decision how long you have to leave the country. If you stay longer than the deadline specified in the decision, you will be in the country illegally.
It is your responsibility to make sure you know what applies to you, so that you leave Sweden within the deadline stated in the decision.
If you have appealed the decision, you have the right to remain in Sweden while your appeal is being examined.
Once the decision has taken effect and can no longer be appealed, you must leave the territories of the Member States and the Schengen area.