Stalin was a notorious opposer of Christianity. In Stalin's day, those who wished to worship had to do so underground in the Soviet Union. Later, the rules were relaxed, and religious life again became open.
Why did the Communist Party permit this?
One obvious answer is control. An underground church is a threat to the establishment, but an open church is controllable. Today, much of that control may be gone, but there is certainly a view that there were many KGB within the hierarchy of the Russian Orthodox church, and that many of the influential patriarchs even today may have both KGB and Communist Party of the Soviet Union credentials.
The following linked article discusses some theories. It should be read with discernment - i.e. with a grain of salt. The person who wrote the article is in a position to know the KGB, but is not in as good a position to know the ROC. That said, the article seems to raise the important issues and ask the tough question: why did the ROCOR (Russian Orthodox Church Outside Russia) reunite with the ROC? Was the ROCOR also infiltrated, is its leadership naive, or has there been repentance/conversion of the KGB members/collaborators who hold office in the ROC?