The Spanish government today won decisive legislative support for its controversial plans to join the North Atlantic Treaty Organization, thus largely clearing the way for Spain to become the 16th member of the alliance as early as next spring.

The NATO membership, which has been a divisive issue in Spanish society, is still opposed by the Socialist and Communist opposition parties, which are preparing anti-alliance demonstrations. Polls show that the majority of Spaniards apparently prefer the nation's current nonaligned status.

The 186-146 congressional vote for NATO membership at the end of a three-day debate must still be affirmed by the Spanish Senate. However, the Senate is expected to approve the lower-house vote before the December meeting of NATO ministers in Brussels, allowing Spain to be formally offered a position in the alliance at that session.

Spain would be the first new NATO member since West Germany joined in 1955.

The government, which has made NATO membership its top foreign policy priority since Premier Leopoldo Calvo-Sotelo took office shortly after an attempted military coup last February, had its narrow working majority in congress buoyed in the alliance debate by the votes of regional and conservative minority groups.

As the debate concluded, however, the Socialist Party was preparing plans for a mass peace rally early in November in Madrid that the organizers said would be aimed both at NATO and the deployment of nuclear missiles in Europe. The rally plans, which follow numerous smaller demonstrations organized by the left, were described as an attempt to embarrass the government on the threshold of its expected pro-alliance victory in the Senate.

The anti-NATO movement in Spain, strong in university and intellectual circles, draws on the country's traditional isolation from Europe, and is reinforced by Spain's nonbelligerent status during the two world wars. Membership in a multilateral military alliance is emotionally rejected by a broad sector of the Spanish population, and public opinion has been further sensitized by the recent antinuclear demonstrations elsewhere in Europe.

A recent poll published in the influential Madrid newspaper El Pais revealed that 69 percent of those questioned believed the NATO issue should be resolved by referendum and not by a parliamentary vote. The Communist Party last week delivered 500,000 signatures to the prime minister's office asking for such a referendum, and a similar petition organized by the Socialist Party is expected to have 1 million signatures.

The newspaper poll indicated that if a referendum were held today, 52 percent of Spaniards would be against NATO membership and only 18 percent in favor, with the remainder undecided. During the congressional debate, Premier Calvo-Sotelo and government spokesmen emphasized that an issue of the complexity of NATO membership could not be put to a plebiscite and that no member of NATO had organized a referendum on the question prior to joining.

Calvo-Sotelo argued that membership in NATO was complementary to the broad Spanish aim of cementing links with Western Europe following the long fascist dictatorship of Gen. Francisco Franco. It was precisely the absence of democratic institutions in Spain during the Franco dictatorship, Calvo-Sotelo said, that had prevented Spain's entry.

The prime minister also stressed that Spain's security was better defended under the multilateral NATO alliance than under the current bilateral agreement with the United States.

The prime minister rejected opposition claims that Spain's NATO membership would increase the country's exposure to a nuclear attack by saying that such risks already existed because of Spain's relationship with Washington.

In carrying its pro-NATO motion, the governing centrist party and its congress allies rebutted opposition attempts to put conditions on the NATO entry, including a prohibition against stockpiling nuclear weapons in Spain and a demand that NATO members, including Great Britain, recognize Spanish sovereignty over the British colony of Gibraltar.