Resource allocation takes place in various types of real-world complex systems such as urban traffic, social services institutions, economical and ecosystems. Mathematically, the dynamical process of resource allocation can be modeled as minority games. Spontaneous evolution of the resource allocation dynamics, however, often leads to a harmful herding behavior accompanied by strong fluctuations in which a large majority of agents crowd temporarily for a few resources, leaving many others unused. Developing effective control methods to suppress and eliminate herding is an important but open problem. Here we develop a pinning control method, that the fluctuations of the system consist of intrinsic and systematic components allows us to design a control scheme with separated control variables. A striking finding is the universal existence of an optimal pinning fraction to minimize the variance of the system, regardless of the pinning patterns and the network topology. We carry out a generally applicable theory to explain the emergence of optimal pinning and to predict the dependence of the optimal pinning fraction on the network topology. Our work represents a general framework to deal with the broader problem of controlling collective dynamics in complex systems with potential applications in social, economical and political systems.