Short answer:
Grammar is syntax and morphology.
Grammar is not semantics, pragmatics, phonology, orthography, or the lexicon.
Long answer:
The definition of grammar in David Crystal's A Dictionary of Linguistics and Phonetics (2008) is quite long and describes a number of different uses of the term. Here is a short excerpt:
[G]rammar refers to a level of structural organization which can be studied independently of phonology and semantics, and generally divided into the branches of syntax and morphology. In this sense, grammar is the study of the way words, and their component parts, combine to form sentences.
This, he says, is the "traditional sense in linguistics", but it's certainly not the only way the term is used. Still, if you were to ask me what grammar is, this is the sense that comes to mind.
I also think this is probably the most useful general definition of the word. If you don't include word formation, then you have a synonym for syntax, but we've already got a word for that: syntax. And if you include everything under the sun, grammar doesn't identify anything in particular.
Using this definition allows us to talk about how grammar interacts with other language components. The Cambridge Grammar of the English Language (2002), pages 3-4, summarizes the interactions like this:
Grammar versus other components
A grammar of a language describes the principles or rules governing the form and meaning of words, phrases, clauses, and sentences. As such, it interacts with other components of a complete description: the phonology (covering the sound system), the graphology (the writing system: spelling and punctuation), the dictionary or lexicon, and the semantics.
Phonology and graphology do not receive attention in their own right here, but both have to be treated explicitly in the course of our description of inflection in Ch. 18 (we introduce the concepts that we will draw on in §3 of this chapter), and Ch. 20 deals with one aspect of the writing system in providing an outline account of the important system of punctuation.
A lexicon for a language deals with the vocabulary: it brings together information about the pronunciation, spelling, meaning, and grammatical properties of the lexical items – the words, and the items with special meanings that consist of more than one word, the idioms.
The study of conventional linguistic meaning is known as semantics. We take this to cut across the division between grammar and lexicon. That is, we distinguish between lexical semantics, which dictionaries cover, and grammatical semantics. Our account of grammatical meaning will be quite informal, but will distinguish between semantics (dealing with the meaning of sentences or words as determined by the language system itself) and pragmatics (which has to do with the use and interpretation of sentences as used in particular contexts); an introduction to these and other concepts used in describing meaning is given in §5 of this chapter.
A grammar itself is divisible into two components, syntax and morphology. Syntax is concerned with the way words combine to form phrases, clauses, and sentences, while morphology deals with the formation of words. This division gives special prominence to the word, a unit which is also of major importance in the lexicon, the phonology and the graphology.
Of course, this isn't the only way the word has ever been used, and it's certainly not the only way the word is used today. For example, you may hear generative phonologists talk about the "phonological component of the grammar". If you're working in that sort of grammatical framework, the short answer I gave at the top isn't right for you.
In fact, grammar has lots of different uses. The complete definition in David Crystal's dictionary is a bit longer than the little quote I gave near the top of this answer, and it covers lots of different uses of the word:
grammar (n.) A central term in linguistics, but one which covers a wide range of phenomena, being used both in mass noun and count noun senses (as ‘grammar in general’ and ‘a grammar in particular’). Several types of grammar can be distinguished.
A descriptive grammar is, in the first instance, a systematic description of a language as found in a sample of speech or writing (e.g. in a corpus of material, or as elicited from native-speakers). Depending on one's theoretical background, it may go beyond this and make statements about the language as a whole, and in so far as these statements are explicit and predictive of the speaker's competence the grammar can be said to be ‘descriptively adequate’ and generative. In the older tradition, ‘descriptive’ is in contrast to the prescriptive or normative approach of grammarians who attempted to establish rules for the socially or stylistically correct use of language. Comprehensive descriptions of the syntax and morphology of a language are known as reference grammars or grammatical handbooks (such as those produced in the twentieth century by the North European grammarians, e.g. the Dane, Otto Jespersen (1860–1943), and more recently by Randolph Quirk et al. (see Quirk grammar)).
A theoretical grammar goes beyond the study of individual languages, using linguistic data as a means of developing theoretical insights into the nature of language as such, and into the categories and processes needed for successful linguistic analysis. Such insights include the distinction between ‘deep grammar’ and ‘surface grammar’, the notion of ‘grammatical categories’ and ‘grammatical meaning’, and the study of ‘grammatical relations’ (the relationship between a verb and its dependents, such as ‘subject of’, ‘direct object of’). In so far as grammar concentrates on the study of linguistic forms (their structure, distribution, etc.), it may be referred to as formal grammar (as opposed to ‘notional grammar’); but formal grammar also refers to the use of the formalized techniques of logic and mathematics in the analysis of language.
Other general notions include the distinction between diachronic and synchronic grammars, based on whether or not grammars introduce a historical dimension into their analysis. Comparative grammar, which compares the forms of languages (or states of a language), relies on a combination of theoretical and descriptive methods. A pedagogical or teaching grammar is a grammar designed specifically for the purposes of teaching or learning a (foreign) language, or for developing one's awareness of the mother-tongue.
The phrase traditional grammar is an attempt to summarize the range of attitudes and methods found in the prelinguistic era of grammatical study. The term traditional, accordingly, is found with reference to many periods, such as the Roman and Greek grammarians, Renaissance grammars, and (especially) the eighteenth- and nineteenth-century school grammars, in Europe and America. It is usually used with a critical (‘non-scientific’) implication, despite the fact that many antecedents of modern linguistics can be found in early grammars. Criticism is directed primarily at the prescriptive and proscriptive recommendations of authors, as opposed to the descriptive emphasis of linguistic studies.
In a restricted sense (the traditional sense in linguistics, and the usual popular interpretation of the term), grammar refers to a level of structural organization which can be studied independently of phonology and semantics, and generally divided into the branches of syntax and morphology. In this sense, grammar is the study of the way words, and their component parts, combine to form sentences. It is to be contrasted with a general conception of the subject, where grammar is seen as the entire system of structural relationships in a language, as in such titles as stratificational grammar, systemic grammar and (especially) generative grammar. Here, ‘grammar’ subsumes phonology and semantics as well as syntax, traditionally regarded as separate linguistic levels. ‘A grammar’, in this sense, is a device for generating a finite specification of the sentences of a language. In so far as a grammar defines the total set of rules possessed by a speaker, it is a grammar of the speaker's competence (competence grammar). In so far as a grammar is capable of accounting for only the sentences a speaker has actually used (as found in a sample of output, or corpus), it is a performance grammar. The study of performance grammars, in a psycholinguistic context, goes beyond this, however, attempting to define the various psychological, neurological and physiological stages which enter into the production and perception of speech. Investigations which go beyond the study of an individual language, attempting to establish the defining (universal) characteristics of human language in general, have as their goal a universal grammar.
Students of grammar are grammarians, and they carry out a grammatical analysis (the term here having no implications of well-formedness, as it has in the notion of grammaticality). When it is necessary to differentiate entities in one's analysis as belonging to a grammatical level of description as opposed to some other (e.g. semantic, phonological), the term ‘grammatical’ is often used attributively, as in ‘grammatical category’ (e.g. gender, case, voice), ‘grammatical gender’ (as opposed to ‘natural gender’), ‘grammatical formative/item/unit’ (e.g. an inflectional ending), ‘grammatical subject/object . . . ’ (as opposed to ‘logical’ or ‘semantic’ subjects/objects . . . ), ‘grammatical word’ (as opposed to lexical word). When a semantic contrast is expressed using grammatical forms, it is said to be grammaticalized (or grammaticized), a process often seen in historical linguistics. An example of grammaticalization (grammaticization) is the use of the motion verb go, as in She is going to London, which has become a marker of tense in It's going to rain. See also application (2), arc, constituent, core, discourse, fuzzy, general (1).
But as detailed as this is, it doesn't cover every way the word has been or is used. I've seen people give definitions as general and vague as "Grammar is what people say". Unfortunately, people simply don't all agree on what grammar means.
Still, I think grammar does have a usual meaning in linguistics, and that meaning is what I put at the top of this answer. The definition I've given is commonly used because it's useful. It lets us make distinctions that help us talk about language. I'll repeat it below:
Grammar is syntax and morphology.
Grammar is not semantics, pragmatics, phonology, orthography, or the lexicon.
Relevant meta discussion. Please read it to see why this post is being asked. – M.A.R. – 2015-10-12T16:47:40.603
Grammar is description how a language system works. – rogermue – 2015-10-24T14:52:50.920
*One month later...*. Might be nice if you actually tried helping to delete a couple of these SINGLE grammar tags. See meta... – Mari-Lou A – 2016-12-16T22:56:53.270