In The Netherlands, starting in 1941, everybody over 15 was required to carry an "Persoonsbewijs (PB)" or ID card. Since someone's religion was part of the basic civic registration, a J was added in the PB. When the Star of David was introduced in 1942, anybody with a J in his PB was required to wear one. I haven't been able to find the punishment for not wearing it. However, wearing one while not a Jew would already result in an arrest and a stay for several weeks in prison or local concentration camp. So you can safely assume that an arrest was certain probably followed by deportation. The fact that religion was part of the basic civil registration is considered one of the main reasons why such an high percentage (75%) of Dutch Jews became victims. Only Lithuania had a higher percentage of Jewish victims. This is also one of the reasons that laws about civil registration by the government are quit strict nowadays. The government can only register data that are relevant for their tasks. Since religion is never relevant for government tasks, registration by the government is effectively forbidden. For church registration purposes the combined churches now have a central registration office that can get data from from the governments registration, not the other way around. You can, as a citizen, forbid information exchange for this purpose.
The answer to this question depends heavily on the where.
In Germany and the Western territories, a lot of it came from either data that already was available. In Germany e.g., it was and still is necessary to register with the Einwohnermeldeamt when you move somewhere and they will also ask you about your religion for census and tax purposes. This is also true for other Western European territories.
Another important source of information where the Jewish communities themselves which had lists of members used by the Nazis to identify Jews and stamp their ID card accordingly (most European countries still require you to have an ID card of some sort and did so in the past). Also, the ID cards were important since without one you were not able to receive food through the rationing system set up in war time.
In countries were the infrastructure of data was less formed or intentionally damaged, the Nazi occupational authrorities often just relied on other inhabitants. Especially in the Soviet Union it was accepted policy to basically just ask who were the Jews or which villages were Jewish and then kill them. Especially outside of Germany but also outside of the Western occupied territories, it basically didn't matter for them if they killed a couple of people they would not consider Jewish. Also, outside of Germany pretty much no distinction was made between Mischlinge and Jews, i.e. people who had less than 3 Jewish grandparents and didn't practice Jewish religion.
As for Jews not wearing the Star of David, this practice was common in Germany, especially in 1942 for it was even more dangerous wearing the Star (you could be snatched by Gestapo driving around town and grabbing people) than not wearing it. If you ended up in some sort of ID card check (and those were rather frequent) or if somebody recognized you, you'd be arrested and most likely deported.
Sources:
Raul Hilberg: The Destruction of European Jews, Oxford 2002.
Richard Evan: The Third Reich at War, London 2009.
Saul Friedländer: The Years of Extermination: Nazi Germany and the Jews, 1939-1945, HarperCollins, 2007.
Götz Aly: Die restlose Erfassung. Volkszählen, Identifizieren, Aussondern im Nationalsozialismus. Berlin 1984.