My son was flying from a Rochester NY to Washington Dulles on flight 4578. They were to land, pickup passengers, then continue to Raleigh. The flight from Rochester was delayed so late arriving in DC. While in the air, the same flight number left DC to Raleigh - obviously different plane. How does that happen?
Yes, and No.
Flight numbers do frequently get used for multiple flights on the same day. For example today UA712 is used for both a Chicago to San Diego flight and a subsequent San Diego to San Francisco flight. In this case, both of these flights used the same aircraft (an Airbus A320, United "ship" number 4616).
On Saturday, UA338 will fly from Salt Lake City to Denver, and then Denver to Dallas Fort Worth - but in this case each of those legs will be flown by a separate aircraft.
Even in cases where the same aircraft is scheduled to fly both legs, delays and changes can mean that different aircraft end up being used for the two flights.
So that certainly means that two aircraft with the same flight numbers can be in the air at the same time. In fact, it's even possible that both flights will be very close to each other (a delayed flight arriving, whilst the on-time one after it with the same flight number is departing from the same airport).
To avoid confusion in situations like this, the flight number that the aircraft uses for the purpose of flight planning, air traffic control, etc will be changed in cases like this - frequently by the addition of a letter after the number.
So if flight UA111 is delayed, then the next segment with the same flight number may be referred to as UA111T, or UA111 "tango" or similar. As a traveler on that flight you won't see this - it'll still be referred to as UA111 because from a traveler perspective there's no confusion - there's still only one UA111 departing so there's no opportunity to (for example) catch the wrong flight.
One exception to this is if a flight is delayed until the next day. In this case there might be two flights with the same flight number departing the same airport on the same day. To avoid confusion here, airlines will either change the flight number completely (one of the XX123's becomes XX234), or they will add a letter similar to above (XX123 becomes XX123B). In this case, boarding passes and airport displays would all show the modified flight number so passengers would know which of the two flights they were on.
It's fairly common for a single flight number to cover multiple flight segments (legs): for example, Qantas flight 1 (QF1) is both Sydney to Dubai and Dubai to London.
In the US, it's apparently common to use the same flight number on separate services flown by separate planes, leading to situations like yours. I don't think this serves any practical purpose except allowing airlines to dupe people into buying "direct" flights that aren't.
In most of the world, the same physical plane flies both legs. However, if the originally planned plane is delayed/cancelled for the first leg, and there's lots of passengers waiting for the connection, and the airline happens to have a plane available at the connection point, it's rare but possible for the second leg to be flown by a different physical plane.
Updated to account for American exceptionalism.
You ask if it's legal. Yes, otherwise as for the reasons explained in the other answers, airlines could not operate delayed or multi leg flights using the number,
To expand on the other answers you need to remember that each plane has its own identifiers, so when an airline books a slot with air traffic control it will be saying,for example, Plane GB-AAA , flying from LHR (London heathrow) to BRS (bristol) operating as Flight Ba1111. So then if for some reason there were delays the airline could then book with air traffic control a second flight Plane GB-BBB flying from LHR (London heathrow) to BRS (bristol) operating as Flight Ba1111. which could be in the air at the same time,
Flight numbers identify the route and scheduled timings for the airline, what air traffic control get is a plane identifier, which has to be unique.