Unfortunately, you can't prevent it.
This has been a problem in game modes like Demolition for at least the past few years.
What people do is, they will bomb one of the sites (or allow it to be bombed) and then gang up and sit right next to where everyone on the other team's spawn points are.
They will kill you as soon as you spawn by standing behind the default spawn locations and shooting or knifing anyone who appears there.
Eventually the higher tier killstreaks unlock, and then in addition to people standing in the spawn, there are choppers and other air support mowing people down left and right.
I tend to avoid these game types for this very reason.
People who play are generally more interested in boosting than in actually playing the game.
However, sometimes it's worth it (ie, to complete a challenge or unlock a title/emblem).
Here's what I would suggest: If you join a game in progress, check the score before you choose your class.
If the score is super lopsided or one or more players have 50+ kills, it's likely to be a group who is spawn trapping, so quit and search again.
If you join a lobby before the game, watch for groups of people with the same tag or who are all in a party together/on each other's friends lists.
It takes a good 3-5 people to properly spawn trap, so it's a common warning sign.
Also check the scores from the previous game if they're still being shown.
Very high scores are also indicative of spawn trapping.
It's my opinion that the gametype is fundamentally broken, and has been for several iterations of the game.
The developers haven't shown any interest in changing it to fix this problem, so if you enjoy Demolition (or similar lobbies) be ready to deal with spawn campers/trappers.
gaming.stackexchange.com/users/13845/agent86
My advice?
Launchers, Semtex, Frags, or other similar explosives.
No spawn camper/trapper can stop an RPG to the face!
That's my counter anyway,
I like my games of demolition and n00bs
like that ruin it...
So goodbye fustration, hello RPG-7 and hilarious final kill-cam
:P
I hope this helps if you ever get trapped, just make a spawn camp class with weapons like RPG, Semtex and an assault rifle or shotgun.
Then add perks like sleight of hand, steady aim and quickdraw :) (I will make this class!
Can't believe I just thought of it!)
gaming.stackexchange.com/users/17815/Bradley
What you basically need to do is just camp out your spawn.
I've played a lot of clans that like to spawn trap and the key is to be the anti spawn trap.
They know your spawns, you must know the positions from which they will attack you.
Portable radar is a must in these situations and will get them to complain.
If you use it right you will know where they are trying to attack your spawn.
Also when in a spawn trap you must not leave your spawn because it is their objective to catch you when you're running.
Camp your spawn and you will do good!
gaming.stackexchange.com/users/20607/Aaron
If you are playing Hardcore, then there's not much you can do in order to avoid the massacre.
On regular mode, try to surprise the opponent by choosing a class like the Riot Shield + pistol + C4.
Keep your shield up, and draw fire while your teammates spawn behind you.
This throws them off rather quickly, and I use it quite often in cases of emergency.
Remember: They are after quick kills.
Make life harder for them.
No one expects the Spanish Inquisition!
gaming.stackexchange.com/users/4182/DrFish
I play with my friends capture the flag all the time and there is no real way to stop it, however we have developed a custom class for this exact purpose.
Two of us have riot shields as our primary weapon and our secondary weapon
is what ever you use most (you have to have overkill equipped)
also equip blast shield, and the martyrdom equipped so you drop a grenade if you die.
when you and your friend spawn with riot shield kneel down right away
so they can't shoot your legs
this will provide cover for your friends who will spawn behind you giving them cover and a chance to break the spawn.
This is the best we could come up with seeing as sometime you spawn in front of the spawn camping that's why its a good thing for everyone to have martyrdom equipped so they have to move seeing as there is six in a team.
P.s me and my friends frown on spawn camping and were thinking of creating a youtube account with clips of all the spawn campers we encounter so everyone can avoid playing with them in general.
gaming.stackexchange.com/users/46469/user46469
I agree with Agent86 that the gametype has some fundamental flaws, most prominently the spawning.
Some suggestions not yet brought up: Class with: Blind Eye Pro - quicker vehicle take down Blast shield
Pro - you'll likely need to shake off a few frags or flash bangs to the face Steady Aim Pro - spray and pray, faster aim after sprinting Launcher - choose your fancy EMP Grenade - an unsung hero of tac grenades (one shot IMS, any sentry, portable radar)
Death Streak - Martyrdom, Death's Hand Shotgun w/ damage, SMG w/ rapid fire - whatever you can use for faster takedown Support killstreak - UAV/CUAV/SAM - some easy points on the board, and anti killstreak
gaming.stackexchange.com/users/21619/EBongo
How can you prevent spawn camping on MW3 in game modes like Demolition? It's very annoying and ends up ruining the game. Is there any good counter to spawn campers?
Spawn trapping and camping on MW3
gaming.stackexchange.com
<call-of-duty-modern-warfare-3>
gaming.stackexchange.com/questions/37053
gaming.stackexchange.com/users/11330/Cem22lp
You can't prevent it. You can try bombing one of the sites with semtex or similar explosives.  Several helpful techniques have been described, although spawn trapping is unavoidable.
What people do is, they will bomb one of the sites (or allow it to be bombed) and then gang up and sit right next to where everyone on the other team's spawn points are.
Evidently spawn trapping cannot be prevented due to some inherent game flaws. Specific weapons are suggested as a solution to the problem. Depending on the difficulty, picking a Riot Shield could be useful. Defending the spawn and being aware of the enemy could be useful.