What is causing these "corners" on this F-104 V-n diagram?
what is causing the &quot;corners&quot; that we see at the following points?

Compressibility. Close to Mach 1 the lift curve slope increases according to the Prandtl-Glauert rule with the factor $\frac{1}{\sqrt{1-Ma^2}}$. Since the X-axis shows indicated speed, the Mach 1 point moves left with increasing altitude. Technically, those corners should also be added for the negative lift limit.

Do these points simply represent the &quot;corners&quot; of what performance is possible while still maintaining horizontal flight?

No. A v-n-diagram covers the whole load factor range while horizontal, steady flight happens along the line y = 1g (except for turns or inverted flight, of course). Adding more thrust will not change the limits. Maneuvering is only possible/permitted (see below) inside the lines and maneuvering  at constant altitude depends on thrust which is not included here. The lines show lift and structural limits; thrust envelopes look different.

Wouldn't it be theoretically possible for a pilot to operate well outside this envelope?

Not really. Momentarily yes, for example the stall lift coefficient moves up with higher pitch rate, but that only works while pitch angle is increasing. Also, at supersonic speed (after all, your example of 340 KIAS @ 40 kft is equivalent to Mach 1.07) the envelope is limited by angle of attack, not lift. Therefore, 4g at Mach 1.07 in 40 kft is outside of the permissible flight envelope but may be reached if the pilot overrides stick shaker and stick pusher (depending on control authority).

So this diagram is really as much about achievable performance as about protecting the aircraft from excess loading?

Yes. The region left of the stall lines is impossible to fly since lift is limited at lower speed. However, the top speed limit might well be exceeded in a dive if the pilot chooses to do so. Equally, the g limit might be exceeded right of the point where the envelope becomes horizontal.
Thank you @JanHudec for pointing out correctly that those areas which can physically be reached by improper flying but might incur damage to the airframe are colored red.