"For the word of God is living and active, sharper than any two-edged sword, piercing to the division of soul and of spirit, of joints and of marrow, and discerning the thoughts and intentions of the heart."
The sword symbolizes discernment.
Dividing what is true from what is false.
Dividing good from evil.
Some want and expect God to love all His children equally and to treat them all equally. God gives them all a chance and is not willing that any should perish. But, after that chance(s) is given, some do not turn to the light or are not drawn/called. Then, justice is required. He is a God of justice and of laws. Therefore it is NECESSARY that judgment be administered and the guilty punished under the laws of man and/or of God.
If this didn't happen, He would not be a God of justice and of righteous laws.