When is it correct to use は but not が, and when is it correct to use が but not は? Are there any times when you can use either without changing the meaning of the sentence? How does switching change the meaning of a sentence?
は and が are a bit complex because they have several meanings, and some of the shades of meaning of wa and ga are a bit hard to distinguish casually.
The best coverage of this that I have read is "The Structure of the Japanese Language" by Susumu Kuno (written in English). I'm going to attempt to synthesize some of that here, but Kuno's explanation is much more complete. I wrote most of this as an answer to a similar question on an internet forum about a year ago, so if you find it there, that's me too.
There are two meanings to は, and three for が. は is used for contrast and thematic reference. が is used as a direct object marker for certain verbs/verbals, an exhaustive subject marker, and as a 'neutral-descriptive'.
は is often called the 'topic marker', in contrast to the 'subject marker' of が, but the key difference is context. Imagine, if you will, a big box of things that have been mentioned in a conversation. We'll call this box the "universe of discourse". Certain constructions can only be used with things that are in this "universe". This sounds ridiculously abstract, until you realize that we do this in English as well.
English Anaphoric Phrases:
The boy was bouncing a ball off the store wall.
is not a valid sentence in isolation.
Now, several of you are freaking out right now, since that seems like a perfectly reasonable English phrase. It is... but not without context. If I start the conversation with that phrase, I've left out enough important details that you'd be reasonable to suspect I was playing a game of quotations (or insane...).
I saw a boy in Wallgreen's last night. The boy was bouncing a ball off the store wall.
provides the needed context. The first sentence introduces a boy into the context of the conversation, the "universe of discourse", and "The" selects him out of it as the particular object I wish to make a statement on.
A boy was bouncing a ball off the store wall in Wallgreen's.
is perfectly reasonable. From this we can see that we use "A" to introduce elements to our "universe of discourse", and "the" to select them out. There are several things that are considered to be always in this universe, such as personal referents. Note my first example is perfectly fine if your listener is aware you have a son. This can get a bit messy in English, so let's head to Japanese before the analogy fails.
In Japanese, the thematic は is used with generic noun phrases ("the brits") or things that are already in the universe of discourse. It's sometimes tricky to nail down exactly what is there, but the general idea is that you don't introduce things to the conversation using thematic は。 This is why you cannot use question words with は, the non-specified referent cannot be in the universe of discourse. (だれは来ましたか?) <--- BAD! INVALID! DO NOT USE!
Contrastive は, on the other hand, is much more free, and this partly explains why 「雨は降っていますが、たいしたことはありません」 is valid, while 「雨は降っています。」 is not.** Note that this is more complicated than the textbook contrastive は, as the contrast extends through the meaning of the final predicate, not just the things before the は marker.
And unfortunately it can be ambiguous which は you're looking at. Kuno's example is 「わたくしが知っている人はパーティーに来ませんでした」. If read as thematic は (if you were talking about all the people you know... such as all your new Japanese friends), it means "Speaking of the people I know, they did not come to the party". If you read it as contrastive, it means "People came to the party, but none that I know."
There can be only one thematic は in a sentence. If you see a second one, the second is certainly contrastive, and the first might be.
On to が... The first meaning of が is trivial, the direct object of certain verbs, particularly those having to do with personal capability or preference, replacing the normal direct object particle を, e.g.「だれが映画が好きですか？」. This is adequately covered elsewhere, and aside from the curious subset of verbs on which this is used, is mostly uninteresting.
Exhaustive listing ga vs neutral description ga
The other two meanings, exhaustive-listing and neutral description, are a bit tricky to understand. Any が can be an exhaustive-listing が, but neutral description only works with action verbs, existential verbs, and adjectives/nominal adjectives that represent state change. "Sentences of neutral description present an objectively observable action, existence, or temporary state as a new event." Neutral description is a valid way of introducing something to the universe of discourse, but it is far from the only one.
For predicates with stative verbs or adjectives/nominal-adjectives of permanent states, が can only be interpreted as exhaustive-listing. Exhaustive-listing works similar to contrastive は, implying contrast to the rest of the universe of discourse.
できる is a non-action verb, so this is exhaustive-listing. Assume that we are talking about the three new students: Jon, Bill and Tom. If B knows that Jon and Tom can both speak Japanese, B just lied. If B knows Jon can speak Japanese, but doesn't know about the others, the contrastive は is appropriate to use instead of が.
Note that this is only a quick overview of the whole topic, and each of these uses has special-cases that bends the rules... but this is a decent summary of the common cases.
** It's very hard to think of a valid way to introduce rain (in a non-general way) to the conversation without it falling. I'm sure someone can dream up a way for it to work, but for the general meaning of "it's raining", は is not correct.
If you already speak a little Japanese, compare these:
Aiko-chan ga suki desu
I like (love) Aiko.
Aiko-chan wa suki desu
Aiko likes it.
I read a lot about this, theory about subjects and objects, but for some reason, this simple example (heard once I'd had a few months in Japan) most helped me to "get it".
In my opinion, it is a bit of a mistake to think of は as being in opposition to が. There are times where は marks things that have nothing to do with the subject of the sentence. There are times where you have to choose between は and を for example.
今はどうしようかな？ = What should I do now? Sometimes the は will be left off, but が could never be used here because it's not the subject (the subject is implied to be the speaker -- or it could be the listener or whoever the context suggests). The function of は in a sentence like this is contrastive... "I'll do such and such tomorrow, but what should I do now?"
As for when は replaces を, consider a sentence such as 魚はよく食べるけど、寿司はあまり食べない。 Here you're contrasting the two things: "As for fish, I eat it often, but as for sushi, I rarely eat it."
Think of は as separate from the subject of the sentence. Think of 私はもう行きます not as "I'm going now", but rather something more like "As for me, I'm going now." Notice how "me" occurs twice in the latter sentence (the second time under the guise of "I", but both are 私). The 私は says "We're gonna talk about me and what I'm gonna do." You could add 私が after it for the "I" in "I'm going" -- but why would you? We already know that you're the one who's going to be going, since "As for me, he's going" would make no sense. I did this in my translation of the fish/sushi example, too; notice the way I worded it when a more natural translation would have been "I eat fish often, but I rarely eat sushi."
Another thing that helps is to imagine sentences as answering questions, even if the questions weren't actually asked. For example, to grasp the difference between 太郎さんは学生です and 太郎さんが学生です, consider that the former answers the question "Who is Tarō-san?" (太郎さんは誰ですか？) and the latter answers the question, "Who is the student?" (誰が学生ですか？).
The book Making Sense of Japanese goes into more depth about this sort of stuff.
I think we should also mention that は is often favored by native speakers in negative statements (but is context-dependent too). This may be a variant of the contrastive-marker function, but you don't always need to explicitly state what it is contrasting.
So in negative statements, the contrast is often implied by は rather than being directly stated.
I like to use this as a guide. は denotes and emphasises what comes before it, whereas が emphasises what comes after it. e.g. If I wanted to say "I am Luke" and wanted to make myself the focus of the sentence, I would say 私はルークです, but if I wanted to make my name the focus instead of myself, I would say 私がルークです.
These sentences have the same English translation - "The cat is blue":
ねこは青いです - the cat is emphasised ねこが青いです - the colour of the cat is emphasised