Why do my friends' and my pets keep despawning?

Only pets that you have actually tamed(horses,cats,dogs,parrots,foxes) or nametagged mobs are marked with the PersistenceRequired tag. Because I assume you are in creative mode

/execute as @e[type=<mob>] run data merge @s {PersistenceRequired:1b}

Run the command after you spawn your mobs, and they should never despawn. And, regarding them not taking damage, you can put this in a repeating command block:

/effect give @e[type=<mob>] resistance 1 4

You might also wanna get rid of the chat hogging, so run

/gamerule commandBlockOutput false