A speaker box does two things. Like was mentioned control or limit cone excursion, and second, acoustically match the speaker to the area it is in.
You can control cone excursion by limiting power to the speaker. A second problem with a car or truck is every time a door is closed it builds pressure in the car temporarily, and pushes the speaker towards the outside of the car.
Acoustically matching the speaker is way more involved. Each speaker has a set of parameters that among other things gives the speaker cone a resonate frequency. A heavier speaker cone will resonate at a lower frequency. A tighter cone suspension on a speaker cone will have a higher frequency. A smaller speaker will have a higher frequency.
Another factor in a speaker box is that when the speaker moves out, it makes a positive pressure in front of the speaker, and a negative pressure behind the speaker. If you do not isolate the back and the front of the speaker from each other, these two pressures tend to cancel, and you lose response at lower frequencies. Additionally, you lose any control on cone excursion.
A closed box of the right size will control cone excursion, but limits low frequency response, because it just damps, or throws away the sound from the back side of the cone.
A ported box, of the right size can give you a lower frequency response in a same sized box, or a similar frequency response in a smaller box, and an increase in audio volume in either case, because a ported box uses the back side of the speaker.
Some speaker manufactures will give you data on each speaker model about what size of box the speaker works with and the frequency you need to tune the ported enclosure to.
I do sound for high school dance competitions. I build a pair of subwoofers for my sound system. The subwoofers are a 14 cubic foot ported box, with two Electrovoice DL-18-W speakers, tuned to be flat down to 34 Hertz. For reference, Low "B" on a five string bass guitar is about 32 Hertz. Low frequency rolloff on the subs is 12 DB per octave.
My sound system in a high school gym.
The speaker stack on the stage left side of the gym.
The speaker stack on the stage right side of the gym.
Close view of the amplifiers. There are three amplifiers I use, the subwoofers are using the bottom amp in the top amp rack. this amplifier is a Fender 2244, FTC rated at 440 watts RMS per channel, into a 4 ohm load. The wood box main speakers use a SAE P-250 amp, FTC rated 250 watts, RMS per channel, into 8 ohms, and the plastic main speakers use a QSC PLX 2402 that is about 500 watts per channel, into 8 ohms. The main speakers have a 15 inch woofer, and a 1 inch compression driver horn. there is a external crossover, set at 160 hertz, with lows going to the subwoofer Fender amp, and the mains get everything above 160 Hertz, and use their internal passive crossover to split the midrange, and treble frequencies.