The one who leads others in prayer is known in Arabic as an imām.
A man may lead both men and women in prayer, but a woman may only lead other women in prayer.
The arrangement of the people praying is as follows:
As for when there are rows of men and rows of women praying in the same room or hall, they should be as far apart as possible, with the men's rows starting at the front, and the women's rows at the back, because of the statement of the Prophet (may the peace and blessings of Allah be upon him):
"The best rows for men are the front ones and the worst are the back ones, and the best rows for women are the back ones and the worst are the front ones." [Ibn Mājah: 1001]
When standing in the rows for prayer, it is important that the row is straight, and that there are no gaps between people. There are numerous narrations regarding the importance of this, including the following narration of Abū Mas'ood (may Allah be pleased with him) that the Prophet (may the peace and blessings of Allah be upon him) said:
"...Make the rows straight and do not differ, lest your hearts differ..." [Muslim: 432]
As well as the narration of an-Nu'mān ibn Basheer (may Allah be pleased with him) that:
"The Messenger of Allah (may the peace and blessings of Allah be upon him) used to straighten our rows, as if he was straightening the shaft of an arrow, until he saw that we had learned it. Then he came out one day and was about to say the takbeer, when he noticed a man whose chest was sticking out from the row. He said: 'Slaves of Allaah! Make your rows straight or Allaah will cause discord among you.'" [al-Bukhārī: 717, Muslim: 436]
As for removing the gaps in the row, 'Abdullāh Ibn 'Umar narrated that:
"The Messenger of Allah (may the peace and blessings of Allah be upon him) said: “Straighten the rows, for you form rows like the angels, and keep your shoulders in line with one another, and fill the gaps, and do not leave any room for the devil. Whoever joins a row to complete it, Allah will take care of him, and whoever interrupts a row, Allah will cut him off." [Abū Dāwood: 666]
It is particularly important that the imām is followed, and that all of the actions of the prayer (with the exception of saying aameen) occur after the imām, without preceding the imām, and without lagging behind. The evidence for this is the narration of Abū Hurayrah (may Allah be pleased with him) that the Prophet (may the peace and blessings of Allah be upon him) said:
"The imām has only been made to follow, so when he says Allaahu Akbar, then say Allaahu Akbar; and when he bows, then bow; and when he says, sami' allaahu li-man ḥamidah, then say, rabbanaa wa lakal-ḥamd; and when he prostrates, then prostrate; and if he prays sitting, then pray sitting altogether." [al-Bukhārī: 734]
With regard to preceding the imām, the following narration of Abū Hurayrah (may Allah be pleased with him) that the Prophet (may the peace and blessings of Allah be upon him) said:
"Does the one who raises his head before the imam not fear that Allah may turn his head into the head of a donkey, or make his form like that of a donkey?" [an-Nasā'ī: 828]
It is sometimes the case that you will wish to join a prayer with a different intention to that of the person leading the prayer. For example, a person may be praying a voluntary prayer, and you wish to join them to pray your obligatory prayer. Likewise, a person may be praying 'Aṣr prayer, and you are travelling and want to pray Dhuhr prayer.
Generally, it is permissible for a person praying an obligatory prayer to lead a person praying a voluntary prayer, and likewise for a person praying a voluntary prayer to lead a person praying an obligatory prayer. It is also permissible to pray a different obligatory prayer than the obligatory prayer which is currently being performed (should you have a valid reason for doing so, such as travelling), and likewise a different voluntary prayer from the voluntary prayer currently being performed.
The evidence for this can be found in the following narrations:
Jābir ibn 'Abdullāh (may Allah be pleased with him) said: "Mu'ādh ibn Jabal (may Allah be pleased with him) used to pray with the Prophet (may the blessings and peace of Allah be upon him), then he would go to his people and lead them in prayer..."[Muslim: 465]
Abū Sa'eed al-Khudrī (may Allah be pleased with him) narrated: "One day the Messenger (may the peace and blessings of Allah be upon him) was sitting with his companions when a man came in after the prayer had finished, and he said: 'Who will give charity to this man and pray with him?' One of the people got up and prayed with the man." [at-Tirmidhi: 220]
Both of these narrations show people leading others with a difference in intention. In the first narration, the person praying a voluntary prayer leads those who are praying an obligatory prayer, and in the second narration, the person praying an obligatory prayer leads a person praying a voluntary prayer.
As for the differences in the prayer itself, there are three situations: