The simplest well-known explanation here is offered by the summary of Ellicott -
(34) Swear not at all.—Not a few interpreters, and even whole
Christian communities, as e.g. the Society of Friends, see in these
words, and in James 5:12, a formal prohibition of all oaths, either
promissory or evidential, and look on the general practice of
Christians, and the formal teaching of the Church of England in her
Articles (Art. xxxix.), as simply an acquiescence in evil. The first
impression made by the words is indeed so strongly in their favour
that the scruples of such men ought to be dealt with (as English
legislation has at last dealt with them) with great tenderness. Their
conclusion is, however, it is believed, mistaken:
Because, were it true, then in this instance our Lord would be directly repealing part of the moral law given by Moses, instead of
completing and expanding it, as in the case of the Sixth and Seventh
Commandments. He would be destroying, not fulfilling.
Because our Lord himself answered, when He had before been silent, to a solemn formal adjuration (Matthew 26:63-64), and St. Paul
repeatedly uses such forms of attestation (Romans 1:9; 1Corinthians
15:31; 2Corinthians 1:23; Galatians 1:20; Philippians 1:8).
Because the context shows that the sin which our Lord condemned was the light use of oaths in common speech, and with no real thought
as to their meaning. Such oaths practically involved irreverence, and
were therefore inconsistent with the fear of God.
The real purpose of an oath is to intensify that fear by bringing the
thought of God’s presence home to men at the very time they take them,
and they are therefore rightly used when they attain that end.
Practically, it must be admitted that the needless multiplication of
oaths, both evidential and promissory, on trivial occasions, has
tended, and still tends, to weaken awe and impair men’s reverence for
truth, and we may rejoice when their number is diminished. In an ideal
Christian society no oaths would be needed, for every word would be
spoken as by those who knew that the Eternal Judge was hearing them.