The EU is not a state and has no real means to compel members by force. Its supranational character lies somewhere between an intergovernmental organization like the UN and a federal state like the US.
The immediate reason why judgements of the ECJ have effect is respect for the rule of law. Member states have transferred limited aspects of sovereignty to the EU. The EU uses this power to pass laws. If a member states passes laws that contradict EU law, or fails to pass laws to implement EU law, they can be sued before the ECJ in an infringement procedure. These are actually fairly common, and have been brought against all kinds of member states. If the ECJ finds that some aspect of a member state's law is invalid, this is also binding for the lower courts in that member state.
Of course, member states might go rogue and fail to respect the rule of law. On the extreme end, blatant disregard for the judiciary would be a coup or a constitutional crisis. On the more subtle end, a government might pack the national courts with sympathizers that would rule more “flexibly” on matters relating to the EU. The EU is currently struggling with a member state that has brought about such a constitutional crisis: Poland's 2015 reforms of their constitutional court. While the ECJ has ruled against parts of the reform, it is yet unclear how Poland's government will respond.
But such struggles must be understood in a political context. Member states are not just EU members, they are also peers with each other. This is important because the most influential EU bodies are not the central Parliament or Commission, but the intergovernmental Councils of member states. Actions that violate EU law and are to the detriment of other EU members cost political influence and goodwill. Currently, Hungary and Poland are pretty isolated due to their anti-EU and anti-human-rights governments. By expending their influence on anti-democratic domestic issues, they will have a harder time convincing other members to adopt favorable policies on EU subsidies – they are quite literally biting the hand that feeds them. In fact, last year other EU members have started working on making subsidies conditional on adherence with EU law.
This might be a deterrent for member states that are net-beneficiaries of the EU budget. But most net-contributors also know that the EU is not a zero-sum game, and that contributions to the EU budget help stimulate domestic economic growth. For example, Germany's export-oriented industry benefits greatly from stability and prospering neighbors, so that there is no rational basis for Germany to stop contributions. The UK did decide to stop contributions through a legal route – exiting the EU.