Secretary of State George P. Shultz said tonight that the need to balance morality with realism explains why the United States has taken an unrelenting tough stance against communism in Nicaragua while following a softer line toward racial segregation in South Africa.

In a speech here to the National Committee on American Foreign Policy, Shultz drew a parallel between the two countries to illustrate his argument that a "passionate commitment to moral principles could be no substitute for a sound foreign policy in a world of hard realities and complex choices."

"We have friends and allies who do not always live up to our standards of freedom and democratic government, yet we cannot abandon them. Our adversaries are the worst offenders of the principles we cherish, yet in the nuclear age we have no choice but to seek solutions by political means," he said in describing the factors that must be balanced against each other in policy decisions.

His speech was intended as a discussion of the philosophy underlying President Reagan's foreign policy. But he also appeared to be answering charges that the administration has been guilty of a double standard in dealing with the leftist Sandinista government of Nicaragua and the white minority government of South Africa.

Shultz, who received the committee's Hans J. Morgenthau Memorial Award, said the administration is as committed to ending apartheid in South Africa as it is to overturning communism in Nicaragua. But, he added, the situations in the two countries involve different circumstances and different challenges to U.S. interests and thus must be dealt with differently.

Shultz characterized the Sandinista government as "a moral disaster" that should be opposed "on both moral and strategic grounds."

But, he said, "There are some in this country who would deny that America has a strategic stake in the outcome of the ideological stuggle under way in Nicaragua today. Can we not, they ask, accept the existence of this regime in our hemisphere even if we find its ideology abhorrent? Must we oppose it simply because it is communist?"

"The answer is we must oppose the Nicaraguan dictators not simply because they are communists, but because they are communists who serve the interests of the Soviet Union and its Cuban client, and who threaten peace in this hemisphere," he continued.

"Had the communists adopted even a neutral international posture after their revolution; had they not threatened their neighbors, our friends and allies in the region, with subversion and aggression; had they not lent logistical and material support to the Marxist-Leninist guerrillas in El Salvador -- in short, had they not become instruments of Soviet global strategy, the United States would have had a less clear strategic interest in opposing them," he asserted.

Shultz called South Africa "one of the most difficult challenges we face today." He added, "Americans naturally find apartheid totally reprehensible. It must go.

"But how shall it go?" he asked. "Do we want to see the country become so unstable that there is a violent revolution . . . . It is not our job to cheer on, from the sidelines, a race war in southern Africa, or to accelerate trends that will inexorably produce the same result."

He called Reagan's policy of encouraging dialogue between whites and blacks "the moral and the practical way to use our influence to encourage a peaceful transition to a just society."

Shultz rejected charges that Reagan did not do enough when he reluctantly imposed limited economic sanctions against South Africa.

"This approach may suffer the obloquy of the moral absolutists -- of those opposed to change, and of those demanding violent change. But we will stick to this course because it is right," Shultz said.