It also partly depends on the farm. With sheep, for hill farms it's not profitable to send lambs off fat, so we sell them as store lambs through the mart, they travel south and fatten before being sold on again. The big supermarkets prefer not to buy directly from the marts, because it means that they have to deal with whole lambs rather than being able to say 'We want X packs of liver and 10X legs', so the finished lambs will be sold to a broker who'll arrange for them to be slaughtered and packed according to orders and the surplus non-supermarket bits sold elsewhere.
With things like milk and eggs, the supermarkets try to go directly to the farms and work on a cost-plus basis - they'll send in a consultant to scrutinise absolutely every aspect of production, advise on how to lower costs, benchmark against other farms they buy from and then issue a contract based on the farm's theoretical cost to produce one unit plus a negotiated percentage.