Upgrades can be purchased at most shipyards, and come in S, M, L , and XL and XXL versions (so an S station can be upgraded to M using an M S upgrade; an M station -> L etc: hence no SXXL-rated upgrade item). Stations can only be upgraded sequentially, one tier at a time. The time taken to gather production optimisation information increases with each tier, meaning that it will take longer to unlock the ability to upgrade further. Upgrades can be applied to stations even if they are part of a complex, allowing compact, high-production complexes to be created.
To apply the upgrade kits you must first own (or hire) a TL ship. Then you can buy the upgrade kit from the shipyard your TL is docked at and deploy it onto the station that needs upgrading. You do this via the Freight bay of your TL, select the upgrade kit to deploy. This will then give a list of available factories to be upgraded, only factories that can take the upgrade kit will be displayed here. If the factory you wish to upgrade is not displayed on the list, then either its not ready for an upgrade, or you have the wrong size upgrade kit.
The upgrade kits do not require you personally to be in the same sector as the factory and TL, but the TL must be in the same sector.
You can view the production optimisation and upgrade program from the Factories Info Menu, when its ready for an upgrade, it'll also display the size of kit that is required, but generally, you need to match the current size of the station with the kit size, so an M station requires an M kit. If the factory or kit does not display a size, then it will be an S.