Before the match, a coin toss is made. The winner of the toss chooses one of three options: to serve first, to receive first, or the side of the court to start from.
If the winner chooses to serve or receive, the other player chooses the side. If the winner chooses the side, the other player chooses to serve or receive.
The winner of the toss often chooses to serve, but may also choose the side with the sun or wind behind them.
Players change ends after the first game and then after every odd-numbered game of each set. They also change ends at the end of a set, if the total number of games in the set is odd.
If the total is even, they change ends after the first game of the next set.
Players change ends so that the sun, wind and court conditions are fair to both.
Example
After games 1, 3, 5 and 7 of a set, players change ends. If a set ends 6-3 (nine games, an odd number), they change ends right after it.
If a player serves out of turn, the correct server takes over as soon as the mistake is noticed. Points already played stay, and a fault that was already hit also stays.
In doubles, if the order of serving was wrong, it stays that way until the end of that game.
It is the job of the umpire to correct the mistake, but players should also check the order.