CZESTOCHOWA, POLAND, SEPT. 20 -- Tens of thousands of workers, many bearing banners of the banned trade union Solidarity, gathered at Poland's most important Roman Catholic shrine here today and cheered two leading clerics as they pronounced a strong defense of the union's ideals.

The turnout for the fifth annual "workers' pilgrimage" to the Shrine of the Black Madonna here was the largest demonstration by Solidarity since the visit to Poland of Pope John Paul II in June and seemed to reflect the increased church commitment to the movement's ideals called for by the pontiff.

The pilgrimage came at a time when Solidarity, still functioning in fragmented, semiclandestine form seven years after its founding as the Eastern Bloc's first independent trade union, is struggling to find its role in a Poland increasingly influenced by the liberal reforms of Soviet leader Mikhail Gorbachev.

In an unusually strong sermon, Bishop Damian Zimon of Katowice said the church continues to believe that "the mass of working people have the right to their own independent and self-governing trade unions" and added that "fundamental economic and political reforms are necessary" in Poland.

The crowd of pilgrims from industrial areas around the country responded with cheers and chanted the union's name when leader Lech Walesa, who sat near the altar on a stage above the crowd, stood, lifted his arms and flashed the Solidarity "V" sign. Church officials estimated the turnout at 100,000, but other estimates put it at 25,000 to 30,000.

As in past stagings of the pilgrimage, which began two years after Solidarity's suppression under martial law, Walesa agreed with church officials not to address the gathering. But the message of the union was seen on scores of banners sprinkled through the crowd, which gathered in a field at the foot of the high walls of the Jasna Gora Monastery, site of the Black Madonna shrine.

"Mary, lead Solidarity out of the underground and the communists out of Poland," read one banner. "We will work only with God and only for Poland," declared another. One sign, referring to the role the shrine is said to have played in repelling a 16th century Swedish invasion, read, "You protected us from the Swedes, Mother, protect us from the Soviets."

Despite the repeated endorsements given Solidarity by John Paul in June and the massive turnout of supporters for a mass in the union's birthplace of Gdansk, Solidarity still appears far from its goal of regaining legal recognition by the government of Gen. Wojciech Jaruzelski.

At the same time, a widespread mood of public apathy, continuing police pressure and the wearing difficulties of illegal organizing have slowly thinned the ranks of union activists. The large underground publishing industry associated with Solidarity also is suffering through a slump, with readership declining and some distributors facing severe financial problems.

Tentative feelers last summer toward talks with Solidarity by the communist-backed unions created after 1982 ended with irritated denials by both sides that any significant exchange had taken place.

An appropriation by the U.S. Congress of $1 million for the union caused controversy here when government spokesmen charged that Walesa and his followers had become "paid agents" of the Reagan administration. The union leadership responded with an announcement that the money would be turned over to a special social welfare fund.

With Jaruzelski now reportedly preparing to unveil a new package of reforms for Poland's economy, some senior union strategists think that Solidarity may have a role to play in pressing for meaningful reforms. The government, they argue, will need the help of the opposition and the church if it is to gain the public support needed to implement a major reform program.

However, most activists are skeptical that Jaruzelski will work with the church or allow the legal appearance of Solidarity or other independent movements. The union's best strategy, these activists say, is to focus on winning influence and exerting pressure for change at the local level -- in factory councils, municipal governments and other grass-roots forums.

"We have to find a way to work in the conditions we have now," said a Warsaw adviser to Zbigniew Bujak, the former Solidarity underground leader who is seeking to organize initiatives in the tractor factory where he once worked. "We can't keep thinking in the terms we did in 1981, because the condition of legal existence in 1981 may never come back."

That the church will continue to support Solidarity's broader goals was made clear today by Bishop Zimon, who argued that the "dangerous apathy" of Polish society would worsen unless authorities "allow the existence of various social organizations that have in mind the common social good." He added, "Public life cannot be limited to people who profess the Marxist world view."

Cardinal Henryk Gulbinowicz, who has presided over the workers' pilgrimage initiative, ended today's mass with a prayer that Poles would learn from Mary "the right to the freedom of man and the freedom of the country."

He then introduced a Lithuanian cleric, Msgr. Olgirdos Gutauskos, who, he said, was visiting the Jasna Gora shrine to give thanks for the 600 years of Catholicism in Lithuania, a Soviet republic that for centuries was united with Poland.

Gutauskos, the administrator of the archdiocese based in Vilnius, a city governed by Poland before World War II, responded to the workers' warm ovation by saying, "It would be good if Lithuanians could applaud as you do."