The Constitutional Tribunal said some EU treaty articles were incompatible with Poland’s constitution.
It ruled that the Polish constitution took precedence over EU law.
Polish judges, it said, should not use EU law to question the independence of their peers.
The legal challenge was brought by Prime Minister Mateusz Morawiecki.
It was the first time in the history of the 27-strong EU bloc that a leader of a member state had questioned wholesale EU treaties in a constitutional court.
Mr Morawiecki wanted to prevent Polish judges from using EU law to question the legitimacy of judges appointed following recent changes to the judiciary.
Those changes have been criticised by the European Commission, the EU’s executive body, and many international legal bodies for undermining judicial independence and increasing political control over courts.
Poland’s Constitutional Tribunal is now dominated by judges that are sympathetic to the governing Law and Justice party (PiS), some of whom are former party members.
The European Commission has already warned it will respond with further legal action against Poland.
Last month, the Commission asked the ECJ to impose daily fines on Poland for its failure to suspend the activities of a new Supreme Court chamber that has the power to sanction judges for the content of their rulings.
It’s important to note the court’s ruling does not take effect until the government publishes it in the Journal of Laws, something it should do within days.
However, it took the government three months to publish the court’s controversial ruling virtually banning legal abortion in Poland.
Lurking in the background is Poland’s €57bn (£48bn; $66bn) recovery plan, which Brussels has yet to approve.
Negotiations are ongoing and the European Commission has said this legal challenge was delaying approval of the plan.
Could this ruling be hard ball tactics by Warsaw to get the commission to agree the much-needed funds? It’s a risky ploy, if it is.