Background image not visible: What am I doing wrong?
Essentially you forgot to show the background images.

In the UI, you do this by pressing on the checkbox in the Properties Panel (N) --> Background Images.



In python, you do this by accessing the show_background_images property of the 3D view region within the 3D view space (within the 3D view area). This is what your space_data variable is referencing.

Another thing you could do (added in the last line below) is move directly to viewing through the camera when the script is done, to immediately see your BG image.

import bpy
filepath = r"C:\test\trees.jpg"
img = bpy.data.images.load(filepath)

rv3d = None
for area in bpy.context.screen.areas:
  if area.type == 'VIEW_3D':
    space_data = area.spaces.active

    rv3d = space_data.region_3d # Reference 3D view region
    space_data.show_background_images = True # Show BG images

    bg = space_data.background_images.new()
    bg.image = img
    break

cam = bpy.data.cameras.new("Camera")
cam_ob = bpy.data.objects.new("Camera", cam)
bpy.context.scene.camera = cam_ob
bpy.context.scene.objects.link(cam_ob)
bpy.context.scene.objects.active = bpy.context.scene.objects["Camera"]

rv3d.view_perspective = 'CAMERA' # Go to camera perspective to see your BG iamge