Some churches teach that baptism is absolutely necessary for salvation. I do not doubt their intentions, but I think this inverts the sacrament from it’s intended purpose. Baptism was given to comfort and assure those who desire God’s grace in Christ.
I want to argue instead that baptism is relatively necessary. While not essential for your justification, Christ calls you to submit to it as the outward initiation into his visible people.
Background on Baptism
Perhaps a helpful way of discussing the relative necessity of baptism in the New Covenant is by seeing its relationship to Old Covenant circumcision. Both signs are basically analogous. They picture Christ's righteousness counted and imparted to all who receive it through faith:
"In him also you were circumcised with a circumcision made without hands, by putting off the body of the flesh, by the circumcision of Christ, having been buried with him in baptism, in which you were also raised with him through faith in the powerful working of God, who raised him from the dead." — Col 2:11–12 11
Notice the spiritual equivalence of the two sacraments: "you were circumcised... having been buried with him in baptism." They each point to Christ's atoning death, albeit in different ways. Jesus was violently "cut off" and "buried" for our sins. The inward application of his death, however, is received personally through faith, for, "you were also raised with him through faith."
In his epistle to the Philippians, Paul writes to predominantly Gentile converts. Although they had been baptized, some might have been tempted to think circumcision was necessary for salvation. Paul assures them however,
"We are the circumcision, who worship by the Spirit of God and glory in Christ Jesus and put no confidence in the flesh." — Phil 2:3
Both signs point to the same promises. What counts is faith in Christ.
Unnecessary for Salvation
God sometimes brings people to salvation prior to receiving the covenant signs. Abraham was justified prior to receiving circumcision,
"Faith was counted to Abraham as righteousness. How then was it counted to him? Was it before or after he had been circumcised? It was not after, but before he was circumcised. He received the sign of circumcision as a seal of the righteousness that he had by faith while he was still uncircumcised. The purpose was to make him the father of all who believe without being circumcised, so that righteousness would be counted to them as well, and to make him the father of the circumcised who are not merely circumcised but who also walk in the footsteps of the faith that our father Abraham had before he was circumcised." — Rom 4:9-12
Likewise, in Acts 10, Cornelius received the Spirit by faith and even spoke in tongues, all before his baptism. Circumcision and baptism are therefore only the outward emblems of God's promise to cleanse and sanctify believers. Faith is the actual instrument of justification before God.
There is a sense in which the covenant signs are necessary. So much so, that during the Old Covenant uncircumcised men were regarded as outside the visible church. Those who stubbornly refused it were understood as lost. The same is generally true under the New Covenant. But as we’ve seen, this cannot owe to any saving power in circumcision or baptism themselves.
You and I cannot peer into hearts to see whether one has faith. Without some visible initiation into the church, how would we determine who to consider a member of Christ’s Body? By appointing sacraments, God sets a visible boundary around his covenant people. And as commands, they are non-optional but not saving in the same way that obeying, “thou shalt not murder,” is non-optional but does not count toward our salvation. Simply put, anyone who openly rejects the outward sign shows contempt for God's word and promises. We can not regard such people confidently as members of the church. God alone knows the reality.
On the other hand, suppose someone is unable to receive water baptism. The thief on the cross, for instance, or someone converted just before martyrdom. It is a great relief to know that God looks upon the heart. Faith and the Spirit unite us to Christ. Water washes the outside of the cup, so to speak, but the Spirit performs inward washing and renewal (Cf. Titus 3:3-8). Therefore, God can impart grace to all who desire it by faith, with or without water.
Baptism is not essential to justification. Nevertheless, it is a necessary step in the path of discipleship to which we are called. Moreover, it is a comforting sign of grace to all who trust the promises. So, to rehash Jesus' word's concerning sabbath, "the sacraments were made for man, not man for the sacraments."