Lebanon's divided parliament today elected Bashir Gemayel, the commander of the country's principal Christian militia and scion of the rightist Phalangist Party, to a six-year term as president over the strong opposition of Moslem and leftist politicians.

The election, held at a military academy outside the capital, was delayed for two hours as Gemayel's aides broke a Moslem boycott by persuading legislators to attend.

Later, as supporters of Gemayel paraded jubilantly through Christian East Beirut, Moslem leaders met in West Beirut shortly after the vote to discuss rejecting Gemayel's election, according to Walid Jumblatt, a leading leftist figure and one of the participants.

Later, however, they issued a communique that did not contest the legality of the election but instead asked for ironclad guarantees that Lebanon's traditional consensus politics between Christians and Moslems be preserved.

Sources said former prime minister Saeb Salam and other key Sunni Moslem leaders agreed tonight to try to meet with the Christians' Lebanese Front, of which Gemayel is a leader.

Jumblatt said the Moslems had considered rejecting the election on the ground that it was "a falsification of true opinion in Lebanon" and had been conducted by intimidation of legislators, 63 of whom had been living in the Christian or Israeli-controlled parts of Lebanon since the beginning of the Israeli siege of West Beirut in June.

The Moslem opposition raised concerns among Western diplomats and Lebanese observers that war-torn Lebanon, which fought a civil war in 1975 and 1976, could be thrown into a serious new constitutional crisis, Washington Post correspondent Loren Jenkins reported from West Beirut.

As the Moslem politicians deliberated into the night, explosions resounded throughout the city as angry leftist militiamen stormed the homes of legislators who had participated in the vote. Homes of at least five deputies, including House Speaker Kamal Assad, were set afire by rockets or blown up with dynamite.

Meanwhile the evacuation of Palestinian guerrillas from Beirut continued, with several hundred more leaving by ship for South Yemen.

Gemayel, 34, who will become Lebanon's seventh and youngest president when he succeeds Elias Sarkis on Sept. 23, is expected to have serious problems in healing the country's wounds, largely because of his hostilities with many of the large power blocs, including the Moslems, the Syrian forces occuping northern and eastern Lebanon and even rival Christian factions. Over the years, however, he has benefited from an informal alliance with Israel that has brought him approximately $100 million in military aid and new power since the Israeli invasion.

Gemayel told Israeli radio after his election today, "This is a big achievement for our democracy, a great day. We hope that what we achieved until now, to reunite the country and to free the country, will continue, and once again the sovereignty will become our own."

The radio station of the Mourabitoun, the major Moslem militia in West Beirut, however, called Gemayel's election "a day of shame" and described it as "the nomination of a military governor for Lebanon by Zionist decree thanks to the Israeli tanks."

President Reagan sent a congratulatory message to Gemayel and another congratulating "the Lebanese parliament in electing the new president through traditional constitutional processes during these difficult and trying times," White House deputy press secretary Larry Speakes said in Santa Barbara, Calif. Speakes added that Lebanon's leadership has "a difficult task ahead."

Gemayel was the only declared candidate in the election. By convention, since it gained independence from France in 1943, Lebanon's president has been Christian and the prime minister and House speaker Moslem. Under Lebanon's constitution, the president has a strong and influential position, with authority to appoint the prime minister and other Cabinet members, propose legislation and execute laws and issue a wide range of regulations.

The election originally had been scheduled for last Thursday at the parliament building, but after an attack on the building Wednesday, it was postponed until today and moved to the assembly hall of a military academy in Fayadiyeh, a Beirut suburb.

The election was scheduled to start at 11 a.m. but by noon only 56 of the 62 deputies needed for a quorum had shown up. Then Gemayel's brother, Amin, who unlike Bashir is a member of parliament, left in his car to find additional members.

At 12:33, a six-car caravan swerved up to the academy's side entrance and two camera-shy deputies were spirited inside by Phalangist bodyguards.

A shouting and shoving match broke out when the bodyguards pushed photographers and television cameramen, shouting, "No pictures!" It was not clear whether the two deputies were there willingly.

At 1:10, the final four deputies needed for the quorum pulled up in a four-car caravan. A minute later, the quorum bell sounded.

The vestibule outside the assembly hall was jammed with bodyguards, most of them wearing silver-plated or gray metal revolvers. Khaki-uniformed police officers and soldiers yelled and screamed at them for five minutes before they were cleared from the hall.

Speaker Assad called the parliament to order at 1:23 p.m. An intense hush fell over the crowded room as each of exactly 62 deputies walked to the front of the room to drop a ballot into the box.

Gemayel fell short on the first ballot, in which a two-thirds majority of the total membership was required, but on the second ballot, in which only a simple majority was needed, he received 57 votes -- 10 more than required.

As he adjourned parliament, Assad congratulated Gemayel on his victory and thanked the legislators for again "fulfilling your obligations in such difficult circumstances."

In the last presidential election in 1976, leftist Moslems, in an unsuccessful bid to prevent a quorum, had shelled the parliament and machine-gunned the cars of the same deputies who voted today.

As legislators, policemen and reporters poured out of the building today, Lebanese Internal Security Forces officers, in their red berets, were firing their handguns in celebration.

The courtyard was filled with Lebanese Army commandos in camouflage dress and the Army's red-helmeted military police.

Jubilant uniformed militamen and women of Gemayel's forces raced through the streets of Christian East Beirut tonight in horn-blaring caravans, riding on the hoods and hanging out of the windows, some firing automatic rifles into the air in a celebration similar to that by Palestinians in Moslem West Beirut Saturday at the start of the PLO evacuation.

It is unclear if Gemayel will be able to reconcile the deep cleavages that remain among the Lebanese since the civil war, particularly because he won his national prominence "leading the Christian charge against the Moslems," as one Western diplomat put it.

A lawyer, Gemayel gave up his practice in April 1975, when it was becoming clear that Lebanon was headed for a Moslem-Christian confrontation. Gemayel is a member of the largest Christian community, the Maronites, and his father, Pierre, founded the right-wing Phalangist Party in the 1930s. Gemayel, who does not have a reputation of being an intellectual, rose through tenacious hard work to lead the Christians' Lebanese Forces militia on the death of its first commander in 1975.

Besides fighting Lebanese Moslems and their PLO allies, Gemayel's troops wiped out the Christian militias of two rival Maronite leaders, Suleiman Franjieh in 1978 and Camille Chamoun in 1980. Franjieh and Chamoun are former presidents of Lebanon.

When Syrian troops entered Lebanon in 1976 as an Arab League force to put an end to the civil war, they initially fought in alliance with the Christians. But this alliance broke down, with Gemayel's forces fighting the Syrians in 1978 and 1981 and Gemayel turning to Israel.

Although Gemayel publicly insists that all foreign forces, including Israelis, must leave Lebanon, critics point out that not once did he criticize the recent heavy Israeli bombing of Moslem West Beirut.

In an effort to gain a wider base of support, Gemayel in mid-July met Jumblatt, one of his bitterest foes. What was supposed to be the beginning of a reconciliation rapidly deteriorated when Jumblatt publicly called Gemayel "the candidate of Israeli tanks."