I'm trying to upgrade a light switch and would be grateful if someone could tell me what goes where. I've tried various combinations but the light either does not turn on or stays on permanently.
Many (not all) smart switches, timers, motion sensors, etc. require a neutral. That is because they need to get power even when the switched device is off. Some older switches were designed to leak power through the device even when "off", but that does not work well with LED lights, so needing a neutral at the switch has become more common in the last few years. Current code in many areas now requires neutral to be available at switches for exactly this reason, but many older houses don't have neutral at many switches.
Your existing switch has hot (black) and switched hot (white).
Your new switch has hot (black), switched hot (red), neutral (white) and ground (green). You have ground available in the box (the uninsulated wire) but you don't have neutral.
There are two basic ways a switch can be wired:
If this is the case, you will either have neutral in the box (connected to the neutral wire coming back from the device) or have it nearby (i.e., neutral didn't actually get into the box but is right next to it), since the hot & neutral from the panel need to be together.
This is a switch loop. Switch loops are a great way to avoid extra wires (saving a little money in installation) by only routing hot & switched hot to the switch and not bothering neutral (until the recent code changes, that is). It looks like you have a switch loop. With a switch loop that doesn't include a neutral, the usual fix is to run a new cable from the device that includes hot, switched hot & neutral. If your wiring is in conduit then you can run just a neutral wire. However, since one of the existing wires is white, that would indicate that the wires are part of a cable and not individual wires because white as an individual wire should only be used for neutral and it is only permitted for a different use in a cable because "all 2 wire cables are black/white".
As Harper noted, there is a way to make this "work" without any additional wires (I won't tell you how) but it is not approved by code and not a good idea.
The wiring in your wall is called a "traditional switch loop". The two wires present (besides ground) are always-hot (we hope, the white) and switched-hot (we hope, the black). Note that neutral is NOT present in this box.
This is a case of white being used as a hot wire because the cable only has 2 conductors. Modern Code requires a re-tasked white wire be marked with paint or tape to indicate it is not a neutral. That is also why they are supposed to make it always-hot; so in all switch positions it will test out as hot.
The switch wants always-hot on black and switched-hot on red. However, it also wants neutral on the white wire. This box does not have a neutral. You cannot use this switch here today.
There are two ways to make this work:
They do exist. They either leak current through the light bulb, which doesnt play well with LEDs, but you can mitigate this by adding a special capacitor made by Lutron IIRC. Or, they received permission from UL (Underwriters Laboratories) to use certain particular tricks which allow them to use ground. This latter type is marked "Requires ground".
This is mandatory for any newly installed switch loops after 2011. In that case, you rewire it so black is always-hot, red is switched-hot, and white is true neutral. The colors will match the switch.
You have an old-style switch loop, where the black wire is the hot, the white wire is the switched hot, and the bare wire is ground, of course. Notice what's missing from this description? Neutral, that's what! This is because a mechanical light switch has no use for a neutral wire; however, your occupancy sensor, like all such sensors and smart-switches, has electronics inside it that need power in their own right. The normal way to provide power to something is with a hot and a neutral, and that's how your sensor does it -- it has its own neutral wire.
However, you don't have that neutral, so you'll need to take that sensor back and get one that doesn't need a neutral -- they'll either be designed and tested so that they can leak a bit of current down ground without creating an undue safety hazard from it, or they'll leak that current through the load, which is fine for incandescents but tends to make LEDs glow/flicker (something that can be fixed by fitting a LUT-MLC from Lutron at the light box). If neither of those are an option, you'll need to replace the existing wiring in the wall with a /3 cable, thus converting it to a new style switch loop that provides a neutral along with the always-hot and switched-hot. This is the way new work is required to be done in most cases as of the 2011 NEC, largely due to the proliferation of smart-switches, occupancy sensors, and other such devices.