One is a loose-fitting droplet barrier; the other is a tight-fitting respirator built to filter the air you breathe.
The short answer, following the comparison published by the Centers for Disease Control and Prevention (CDC), is fit and filtration. A surgical mask is a loose-fitting disposable face covering that creates a barrier against large droplets and splashes but does not seal to the face, while an N95 respirator is designed to fit very tightly, forming a seal, and to filter airborne particles — including small particles — as the wearer breathes.
Because the two products look superficially similar, they are often treated as interchangeable. They are not. They are tested differently, intended for different uses, and protect in different directions: the surgical mask mainly helps contain the wearer's respiratory emissions and shields against splashes, while the respirator is built to protect the wearer from what is in the surrounding air. The CDC's infographic comparing the two is linked in the references below.

A surgical mask — also called a medical mask or procedure mask — is a flat, pleated, disposable covering held on by ear loops or ties. It sits loosely over the nose and mouth. Its job, in the CDC's comparison, is to help block large-particle droplets, splashes, sprays, and splatter that may contain germs, and to reduce the wearer's own respiratory emissions reaching other people.
The loose fit is its defining limitation. Because the edges do not seal against the skin, air can leak around the sides when the wearer inhales, so a surgical mask is not considered respiratory protection against small airborne particles. It also does not filter all small particles from the air. Its strengths are simplicity, low cost, disposability, and effective splash and droplet barrier performance in everyday and clinical settings.
An N95 respirator is a respiratory protective device designed to achieve a very close facial fit and efficient filtration of airborne particles. The designation matters: N95 respirators are tested and approved to filter at least 95 percent of airborne particles under their certification conditions. The molded or structured shape, head straps rather than ear loops on most models, and an adjustable nose piece all exist to create and maintain a seal.
That seal is what separates a respirator from a mask. When an N95 fits properly, the air the wearer breathes is drawn through the filter material rather than around the edges, which is why fit testing and a user seal check are part of proper respirator use, and why facial hair that breaks the seal undermines performance. A respirator that does not seal is not delivering the protection its rating promises.
The CDC comparison sets the two products side by side on the points that matter. Surgical masks are cleared as medical devices for barrier performance but are not tested for fit and are not approved as respirators. N95 respirators are evaluated, tested, and approved under respirator standards, are intended to reduce the wearer's exposure to particles including small particle aerosols and large droplets, and require proper fitting to work as designed.
Intended use follows from that engineering. Surgical masks suit settings where droplet and splash protection and source control are the goal. N95 respirators suit settings where inhaling small airborne particles is the concern and where a fitted respiratory protection program can support their use. Neither product is reusable in the way a cloth covering is: surgical masks are single-use disposables, and respirators have their own manufacturer instructions for use and replacement.
Choosing between the two starts with the question of what needs protecting, and in which direction. To shield against splashes and to contain your own droplets around others, a surgical mask does its intended job. To reduce what you inhale from the surrounding air, only a properly fitted respirator is designed for that task. Product appearance is a poor guide — some masks look substantial yet remain loose-fitting barriers.
For the authoritative comparison, consult the CDC infographic linked below, which was retrieved from the CDC website on May 18, 2020; visit the CDC website for any updates, since guidance and documents can change. The references also link to guidance on the best fabrics for protective face masks, which explains how materials such as melt-blown and spunbond nonwovens contribute to filtration in mask construction.

Not in the way a respirator does. A surgical mask is a loose-fitting barrier that helps block large droplets, splashes, and sprays, and it reduces the wearer's emissions reaching others. Because it does not seal to the face, air leaks around the edges, so it is not considered respiratory protection against small airborne particles.
N95 is a respirator rating: the device is tested and approved to filter at least 95 percent of airborne particles under certification conditions. To deliver that performance, an N95 must fit very tightly and form a seal against the face, which is why fit testing, a seal check, and a smooth seal against the skin matter.
Only where droplet and splash barrier protection is the actual goal. The two products are tested and intended differently: a surgical mask does not seal and is not approved as a respirator, while an N95 is designed and approved to filter inhaled air. Substituting one for the other where respiratory protection is required leaves the wearer without the designed protection.
You may want to try these sections here on Apparel Search: