Updated:

Iron golem

EditionCommand
Java/summon minecraft:iron_golem ~ ~ ~ {PlayerCreated:1b}
Bedrock/summon iron_golem ~ ~ ~ ~ ~ minecraft:from_player

PlayerCreated:1b marks it as player-built, so it never attacks players. Without it, it acts like a village golem and chases players who hit villagers. On Bedrock the minecraft:from_player spawn event does the same.

It has 100 health (50 hearts) and attacks hostile mobs on its own.

Snow golem

EditionCommand
Java/summon minecraft:snow_golem
Java, no pumpkin/summon minecraft:snow_golem ~ ~ ~ {Pumpkin:0b}
Bedrock/summon snow_golem

It throws snowballs at hostile mobs. Snowballs do almost no damage except to blazes. It leaves a snow trail, which is handy for snowball farms. Hot biomes such as deserts and the Nether, and rain, damage it until it melts. Pumpkin:0b shows it without its pumpkin head.

Copper golem (Java 1.21.9+)

/summon minecraft:copper_golem

It takes items out of copper chests and carries them to nearby chests. It oxidizes over time and eventually stops moving; wax it with honeycomb to stop that. It is a recent mob, so check your version if the ID is not found.

Build them in survival

Place the pumpkin last. For an iron golem, the two spaces under the arms must be empty.

Keep a golem still

/summon minecraft:iron_golem ~ ~ ~ {PlayerCreated:1b,NoAI:1b}

Add NoAI:1b for a statue (Java). It will not fight either. See the NoAI guide.

When it does not work

Fix an existing golem (Java)

/data merge entity @e[type=minecraft:iron_golem,sort=nearest,limit=1] {PlayerCreated:1b}

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.