They typically do this one of two ways: (1) through a special product and/or process (e.g. making asphalt out of extremely heavy local crude) or (2) by taking a cut off of a local supply of crude in areas where there is a reasonable demand for refined products but where it is expensive to ship product back from lower cost refineries (e.g. Alaska).
Generally speaking, however, the larger the refinery the more the owner can achieve economies of scale and spread out corporate overhead and thus the better the margins.