How can I rename selected objects instead of just the active object?
You can do something like this:
import bpy

for ob in bpy.data.objects:
    if ob.select_get():
        #ob.name=ob.material_slots[0].name #this can fail if no material is assigned
        print(ob.name)

Or using context as @batFINGER suggests
import bpy

for ob in bpy.context.selected_objects:
     #ob.name=ob.material_slots[0].name #this can fail if no material is assigned
     print(ob.name)