We examine the impact of international trade on aggregate productivity. We show theoretically and numerically that bilateral and unilateral export liberalization increase aggregate welfare and productivity, while unilateral import liberalization can either raise or reduce them. However, all three trade reforms have ambiguous effects in the presence of resource misallocation. Using unique new data on 14 European countries and 20 manufacturing industries during 1998-2011, we empirically establish that exogenous shocks to both export demand and import competition generate large gains in aggregate productivity. Decomposing these gains, we find that both trade activities increase average firm productivity, but export expansion also reallocates activity towards more productive firms, while import penetration acts in reverse. We provide evidence for two adjustment mechanisms. First, both export and import exposure raise the minimum productivity among active firms. Second, efficient institutions, factor and product markets amplify the productivity gains from import competition, but dampen those from export expansion. We conclude that the effects of globalization operate through a combination of productivity-enhancing firm selection and reallocation across firms in the presence of resource misallocation.