Marion Stein has a good explanation over at Happy Nice Time People:
They go to a “desanctified church” where Mary announces she’s found
“the heart of the conspiracy.” A group dressed in pointed sheets, not
white sheets but nevertheless clan-like, is chanting. Why the KKK
reference? It’s another nod to the source material, The Five Orange
Pips. Only here it isn’t southern white men hiding under the sheets.
It’s British women, including Molly Hooper, and John’s housemaid –
women who’ve been abused, brutalized, and ignored. Sherlock’s
fake-girlfriend from last season is there too. They have become the
“invisible enemy” that Mycroft spoke about, and their cause is just.
Sherlock works it out. Mrs. Riccoletti was dying of tuberculosis. She
went on a suicide mission. First, she faked her death by appearing to
shoot herself in the head, but she really shot one of her guns at the
ground while a co-conspirator splashed blood on the back of her head.
A substitute corpse was taken to the morgue. Later, the real Mrs. R
killed her husband, and then shot herself. Her body replaced the
substitute. Thus the legend of the vengeful ghost was born, and the
co-conspirators continued their mission to assassinate the brutes.