I believe the 45 degrees is not an exact number, the actual optimum varies with the particular airplane, but 45 degrees is pretty close for most GA aircraft. I don't have a reference though, I'm afraid. I wouldn't recommend trying this in an engine-failure-at-takeoff situation unless you've practiced it in that aircraft at altitude and know how much height it's going to use up. Better to have a controlled crash flying forward than an uncontrolled impact trying to turn back with high bank, high rate of descent and possible stall/spin.
45 degrees comes from the formula for the altitude lost in a gliding turn. I can't do equations justice on this site, but it's:
(dh/dΨ)=CD/CL^2 *4W/ρSg *1/sin2φ
dh/dΨ is the derivative of altitude with respect to heading. Basically how much the altitude changes with respect to time versus how much the heading changes with respect to time. Essentially, this describes how much altitude you lose in the process of the turning. We are looking for which values make this derivative the smallest.
On the other side of the equals sign are basically three sets of factors. First is the Coefficient of Drag divided by the Coefficient of lift squared. Both of these are functions of angle of attack, and that number is smallest when CL is largest. In this case that occurs at stall speed, since the critical angle of attack is the maximum lift angle of attack. Now, at the critical angle of attack CD is also relatively large... but because the CL term in the denominator is squared, it's the dominant term of that part of the equation. So that's where the idea to do the turn at 5% above stall speed comes from. Stall speed would really by ideal... but it would also provide 0 room for error to prevent entering a stall.
The middle set of terms is 4 times the Weight, divided by rho (air density) times S (surface area of the wing) times g (acceleration due to gravity). Generally, there is nothing we can do to change any of these parameters in flight, so there really is nothing to manipulate here.
So we're on to the last term, 1/sin2φ. Phi (φ) is the bank angle, so this is the term that's going to give us the optimum bank angle. We are looking for the value that makes 1/sin2φ the smallest, which means we are looking for the value of phi that makes that sin2φ in the denominator the largest. Well, sine oscillates from -1 through 0 to 1. At 0, the 1/sin2φ quantity is undefined, and numbers that approach 0 would make 1/sin2φ approach either positive or negative infinity. If that doesn't make sense mathematically, it may make sense physically. If the bank angle is very close to 0, how long will it take to do a 180 degree turn? Basically forever, right? So how much altitude would you lose before you finished the 180 degree turn? All of it... no matter what altitude you started at, because you're not really turning.
So what we're looking for is for sin2φ to equal 1, which is the largest value it CAN equal. That occurs at sin(90 degrees). So that means 2φ=90 degrees, and therefore φ=45 degrees exactly.
So the 45 degrees is exact, at least for the way they modeled this problem (basically you lose the engine, and are instantaneously at the airspeed and bank angle you want for the turn). That's probably a reasonable assumption shortly after takeoff, because you probably aren't very far off of the stall speed anyway. 1.3 Vstall for 0 bank, something like that. Just banking the airplane to 45 degrees will put you below stall speed (45 bank stall speed is 1.414 * 0 bank stall speed) so you would actually have to gain a couple of knots as you rolled into the bank to make this work.
For the scenario I mentioned, where you are well above either 0 bank stall or 45 bank stall speeds, I think the analysis needs to be reconsidered. I just haven't thought it all the way through yet.
Still, I think the last part of your advice stands... This doesn't sound like a maneuver to attempt the first time during the emergency unless the straight ahead options are just awful. I would want to practice it at altitude a bunch of times first (or in a really good simulator), before I considered using it in an actual scenario.