Vertex algebras are algebraic objects that encapsulate the concept of operator product expansion from two-dimensional conformal field theory. Vertex algebras are fast becoming ubiquitous in many areas of modern mathematics, with applications to representation theory, algebraic geometry, the theory of finite groups, modular functions, topology, integrable systems, and combinatorics. This book is an introduction to the theory of vertex algebras with a particular emphasis on the relationship with the geometry of algebraic curves. The notion of a vertex algebra is introduced in a coordinate-independent way, so that vertex operators become well defined on arbitrary smooth algebraic curves, possibly equipped with additional data, such as a vector bundle. Vertex algebras then appear as the algebraic objects encoding the geometric structure of various moduli spaces associated with algebraic curves. Therefore they may be used to give a geometric interpretation of various questions of representation theory. The book contains many original results, introduces important new concepts, and brings new insights into the theory of vertex algebras. The authors have made a great effort to make the book self-contained and accessible to readers of all backgrounds. Reviewers of the first edition anticipated that it would have a long-lasting influence on this exciting field of mathematics and would be very useful for graduate students and researchers interested in the subject. This second edition, substantially improved and expanded, includes several new topics, in particular an introduction to the Beilinson-Drinfeld theory of factorization algebras and the geometric Langlands correspondence. The book is suitable for graduate students and research mathematicians interested in representation theory, algebraic geometry, and mathematical physics.