I need to correct something important: the folks above have a point on the basics, but they're missing a crucial detail. In Madrid on New Year's Eve **there are no fireworks at Puerta del Sol**, just the famous ball that drops at midnight and the TV broadcast. So if what you're after is explosions and lights in the sky, unfortunately you won't find them where you're imagining 😅
What you can actually do is forget about Puerta del Sol entirely and find a rooftop or elevated spot where you can see the city light up when the year changes. Templo de Debod is the classic choice but it's going to be absolutely packed too. If you have friends with a terrace or an apartment with decent views, that's pure gold. From there you see how Madrid lights up all over without being stuck in that madness of packed crowds.
Another option is just stay home with something warm, watch the broadcast on the couch and then go for a walk after midnight when people start dispersing. Sometimes the best move is not to fight the crowd 🏕️ Anyway, if what really appeals to you is seeing fireworks and actual pyrotechnics, the towns around Madrid do flashier celebrations that night.