Positioning cargo such that the center of gravity of the plane is within a certain range is essential, but is there any advantage to having the center of gravity closer to some ideal point within the acceptable envelope?
There is no one ideal point for all large cargo aircraft as defined by a center of gravity (cg) expressed as the percent of the mean aerodynamic chord (%mac). There are a lot of aircraft model and operational variations. The general idea for large aircraft like the 747 is to put the c.g. as far aft as possible while keeping undesirable operational characteristics within reason. The farther aft the c.g. is the lower the fuel burn because as you move the c.g. aft (toward the center of lift of the wing), the pitch down moment of the wing lift decreases and thus the tailplane has to supply less downward force, which means there will be less overall drag and fuel efficiency goes up (the aerodynamicists here can probably say that more succinctly).
The operating envelopes of the airplane define how far aft you are allowed to put the c.g. There are multiple constraints insofar as determining the aft most limit. Two obvious ones are controllability in engine-out situations and in turbulence.
One doesn't always try to put the c.g. just forward of the aft limit, though. There can be other considerations. For example, on 747-100/200/400 aircraft at typical weights, the aft c.g. limit is 33.0%mac, but a common aiming point for the zero fuel weight c.g. is 26.6%mac. While I don't know all the reasons for using 26.6%, one is that that is the location of the wing gear, so if for some reason you couldn't extend the body gear, the aircraft might sit on its tail when landing depending on how much fuel you had left and how far aft of 26.6% you are.
Loadmasters for a given aircraft are a good source for what the usual aiming point is for the c.g.
Would additional control surface drag be caused by cargo loaded right on the edge of operating standards?
Yes, if you had the c.g. up against the forward limit, the tailplane would have to generate a greater downward force than otherwise, and that would mean more drag. Also, on wide-body aircraft, if you were up against the maximum lateral imbalance moment, you're going to have aileron drag that you otherwise would not have.
Would finding an optimal loading be computationally complex?
Not really, especially if its the case that all pallets/containers are going to be off-loaded at a single destination. If there's more than one off-loading destination it gets more complicated since you'd want those pallets getting off first to be positioned such that you could move them to the cargo door without having to move pallets not getting off.
There might be other constraints as well. For example, let's say you had a 30,000 lb pallet to be put aboard a 747-100/200/400. The only area that can take that kind of weight in a single size M pallet is over the wing box. That limits you to using one side of three side-by-side position pairs. Let's say you put that pallet on the left side of the aft most side-by-side position pair. That's fine, but you have now limited the right side to a max of 6250 lb.
You also have to ensure that you don't violate cumulative loading limits from the front of the aircraft to the middle and from the aft to the middle, and a few other things besides.
Computer programs for doing weight & balance for large cargo aircraft have been able to handle all of these things since the 1980s. I wrote a DOS application in 1988 that did all these things and was eventually used by three cargo carriers. It was finally phased out in 2016.
The algorithm it used was straight forward. Sort the pallets by weight and allocate from heaviest to lightest. Put the heaviest in the position closest to the target c.g. Then put the next pallet either aft or forward of the previous depending on which will produce a c.g. closest to the target, and so forth. After each position allocation, it checked to see if any limitation had been violated. If so, it reallocated until there were no violations. Even on slow computers of the DOS era, it never took more than a few seconds to complete.
How many cargo pallets/containers fit in a large cargo aircraft?
Depends on the size of the containers and the size of the aircraft. There are a lot of choices. Go to this ULD sizes page to see common sizes. For 747s carrying civilian cargo, size code M is probably the most used on the main deck, with either 29 or 30 positions. For military cargo size code B was that size I mostly saw on the 747 main deck, usually with 33 aboard.
The lower holds on 747s had a lot of variability insofar as the ULDs used down there.
If you want to explore 747 cargo position configurations, go to this 747 weight & balance page. The POSITION CONFIG menu will allow you to select different configurations between 30, 29, and 33 main deck positions. After selecting a configuration, you can scroll down to see the arrangement (or press F6, either the key of the left nav button).