The entries for all the golems in the MM say that golems
have magic immunity, which is supposed to allow golems to
completely resist most magical and supernatural effects
except for specific ones listed in each golem’s description.
(Most of those heal, slow, or damage the golem.) Can you
cast beneficial spells on a golem? For example, can you turn
a golem invisible? Can you teleport one? Could you cast
darkness on a golem? How about fly or reverse gravity? To
put it another way, if you are not trying to directly cause
damage or drastically alter the golem (such as with
polymorph), will the spell work? Clearly, if you cast
darkness on yourself and the golem attacks you, once it
moves into the area of darkness, it can no longer see you.
But can you cast darkness on the golem? Could a golem use
a magic item, such as a ring of invisibility?
As noted in each golem’s entry, a golem resists any spell or
spell-like ability that allows spell resistance. (In previous
versions of the D&D game, golems were impervious to most
supernatural effects as well, but that is no longer the case.) In
most cases, you can tell if a spell or spell-like ability works on
a golem simply by looking up its spell resistance entry. Let’s
take a quick look at the effects you’ve listed:
Invisibility: You cannot turn a golem invisible with any
version of the invisibility spell. (They all have spell resistance
entries of “yes.”) This immunity to being made invisible
doesn’t mean the golem has any special ability to see or detect
invisible creatures or objects.
Teleport: The spell resistance entry for the various versions
of the teleport spell is “no,” at least for creatures. Note that
when you’re using the teleport spell, you teleport yourself and
other willing creatures. Unless you can command the golem
(and you probably don’t unless you created the golem), it
probably isn’t willing to accompany you and receives a saving
throw to resist.
Darkness: You can’t cast a darkness spell on yourself, on a
golem, or any other creature—only on an object (see the spell
description). Golems have no special ability to see through
darkness (and the spell resistance entry is “no”). The silence
spell provides a better example of how a golem’s magic
immunity (and spell resistance in general) works. You can cast
silence on a creature, and when you do so, spell resistance
applies (see the spell description). A silence spell automatically
fails if you try to cast it on a golem.
Once a silence spell is operating, silence reigns throughout
the emanation the spell creates. If a golem moves into the
emanation, the golem still cannot hear or make any noise.
Fly: You can’t use a fly spell to make a golem fly (the spell
resistance entry is “yes”).
Reverse Gravity: The spell resistance entry is “no,” so it
affects a golem as readily as it affects any other creature or
object.
Note that a golem cannot voluntarily lower its magic
immunity so that it can receive a harmless spell, but a golem’s
magic immunity does not extend to magic the golem uses on
itself (just as spell resistance doesn’t apply to such effects). A
golem can make use of any magic item that works continuously
or is use activated (provided whoever commands the golem is
on hand to put the item on the golem or order the golem to pick
it up). Being mindless, a golem cannot use any item activated
by command, spell, or spell completion.