A codeshare occurs when two airlines realize that there's a specific benefit to working together and sharing their "codes" on each other's flights. Similar to an alliance, codesharing helps generate more business for the airline, especially when an alliance doesn't have flights flying to a specific destination, and gives travelers more options.
For example, although Cathay Pacific flies to Toronto and Vancouver, it doesn’t offer flights to smaller destinations in Canada, and neither do any of its oneworld alliance partners. For example, if you wanted to fly from Hong Kong to Calgary, you could fly Cathay Pacific to Vancouver, but then you’d have to book a separate flight from Vancouver to Calgary. By codesharing with Air Canada even though they’re in different alliances, Cathay Pacific can give its passengers access to more destinations.
Sometimes on your ticket, you'll notice an "operated by" beneath your itinerary. This text is how you'll know you're on a codeshare flight (although you'll also be informed of this when you first purchase your ticket). For example, you may have booked a flight to Hawaii on American Airlines, but you'll see "Operated by Hawaiian Airlines" somewhere on your ticket.
If you're wondering about the baggage policy across different airlines: the policy of the carrier operating the flight (or the first carrier, if you have connections on multiple airlines) is usually the policy that applies.