Each EU country has its own social security laws. The obligations and rights under these laws are the same for all workers in that country, whether local or from abroad.
However, EU rules coordinate the national systems, to make sure people moving to another EU country do not lose their social security cover (for example pension rights and healthcare) and always know which national laws apply to them.
Under EU rules, a person can be subject to only 1 country's social security laws at a time - so they must pay their social security contributions in that country only.
Generally, the laws of the country where the person actually works (as an employee or self-employed) apply, and contributions must be paid there. It doesn't matter where the person lives (for example if they commute) or where their employer is based.
There is an exception for workers on postings abroad - if less than 2 years : they can stay insured and pay contributions in the country from which they are posted.
For people working simultaneously in more than 1 country, specific rules determine which country's laws apply and where they should pay contributions.
Dig deeper, country by country: