There could be some differences to this answer depending on the exact society that you are talking about, but the general idea is the same for all societies. Basically, rulers used religion to legitimize their rule by saying either that they were gods or that the gods had selected them to rule.
Some rulers claimed that they were actually gods. Two examples of these are the pharaohs of Egypt and the emperors of Japan. By claiming that they were gods, they were clearly legitimizing themselves. If they were gods, then clearly they had the right to rule.
Some rulers claimed that the gods had selected them to rule. A good example of this was the European idea of the “divine right of kings” or the Chinese idea of the “mandate of heaven.” These rulers were legitimizing themselves because they were saying that it was the will of God (or the gods) that they ruled.